How can i configure JSON format indents in ASP.NET Core Web API

.NET Core 2.2 and lower: In your Startup.cs file, call the AddJsonOptions extension: services.AddMvc() .AddJsonOptions(options => { options.SerializerSettings.Formatting = Formatting.Indented; }); Note that this solution requires Newtonsoft.Json. .NET Core 3.0 and higher: In your Startup.cs file, call the AddJsonOptions extension: services.AddMvc() .AddJsonOptions(options => { options.JsonSerializerOptions.WriteIndented = true; }); As for switching the option based on … Read more

Document.Ready() is not working after PostBack

This will be a problem with partial postback. The DOM isn’t reloaded and so the document ready function won’t be hit again. You need to assign a partial postback handler in JavaScript like so… function doSomething() { //whatever you want to do on partial postback } Sys.WebForms.PageRequestManager.getInstance().add_endRequest(doSomething); The above call to add_endRequest should be placed … Read more

Unable to resolve service for type ‘System.Net.Http.HttpClient’

I had a similar problem – the problem was in double registration: services.AddHttpClient<Service>(); services.AddSingleton<Service>(); // fixed by removing this line Similar examples [just adding to clarify that it’s not specific to AddSingleton, nor related to the order.] services.AddScoped<IService, Service>(); // fixed by removing this line services.AddHttpClient<IService, Service>();

How to get current user who’s accessing an ASP.NET application?

The quick answer is User = System.Web.HttpContext.Current.User Ensure your web.config has the following authentication element. <configuration> <system.web> <authentication mode=”Windows” /> <authorization> <deny users=”?”/> </authorization> </system.web> </configuration> Further Reading: Recipe: Enabling Windows Authentication within an Intranet ASP.NET Web application

ASP.NET core JWT authentication always throwing 401 unauthorized

Keep in mind that the UseAuthentication, UseRouting and UseAuthorization middleware must in correct in order for the ASP framework properly inject the identity context to http request. It should look like this: (.NET Core 3.1) Edit: the same code applies to .NET 5 & .NET 6 app.UseAuthentication(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); });

How to validate uploaded file in ASP.Net Core

You could custom validation attribute MaxFileSizeAttribute like below MaxFileSizeAttribute public class MaxFileSizeAttribute : ValidationAttribute { private readonly int _maxFileSize; public MaxFileSizeAttribute(int maxFileSize) { _maxFileSize = maxFileSize; } protected override ValidationResult IsValid( object value, ValidationContext validationContext) { var file = value as IFormFile; if (file != null) { if (file.Length > _maxFileSize) { return new ValidationResult(GetErrorMessage()); … Read more

ASP.Net MVC 4 Form with 2 submit buttons/actions

That’s what we have in our applications: Attribute public class HttpParamActionAttribute : ActionNameSelectorAttribute { public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo) { if (actionName.Equals(methodInfo.Name, StringComparison.InvariantCultureIgnoreCase)) return true; var request = controllerContext.RequestContext.HttpContext.Request; return request[methodInfo.Name] != null; } } Actions decorated with it: [HttpParamAction] public ActionResult Save(MyModel model) { // … } [HttpParamAction] public ActionResult … Read more

How to add a “confirm delete” option in ASP.Net Gridview?

This should do it. I found it here: <asp:TemplateField ShowHeader=”False”> <ItemTemplate> <asp:ImageButton ID=”DeleteButton” runat=”server” ImageUrl=”~/site/img/icons/cross.png” CommandName=”Delete” OnClientClick=”return confirm(‘Are you sure you want to delete this event?’);” AlternateText=”Delete” /> </ItemTemplate> </asp:TemplateField>