Asp.net web api exception only after deploying at IIS : A route named ‘HelpPage_Default’ is already in the route collection

I just ran into the same issue. I think the error was caused because I had pushed another solution to my site previously and there were leftover files that were somehow getting in the way. To fix this I checked the box that says “Remove additional files at destination” while publishing through Visual Studio to …

Read more

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(); });

Get response from PostAsJsonAsync

Continue to get from content: var httpClient = new HttpClient(); var response = httpClient.PostAsJsonAsync(posturi, model).Result; bool returnValue = response.Content.ReadAsAsync<bool>().Result; But, this is really naive approach for quick way to get result. PostAsJsonAsync and ReadAsAsync is not designed to do like this, they are designed to support async await programming, so your code should be: var …

Read more

Make names of named tuples appear in serialized JSON responses

For serializing response just use any custom attribute on action and custom contract resolver (this is only solution, unfortunately, but I’m still looking for any more elegance one). Attribute: public class ReturnValueTupleAttribute : ActionFilterAttribute { public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) { var content = actionExecutedContext?.Response?.Content as ObjectContent; if (!(content?.Formatter is JsonMediaTypeFormatter)) { return; } var …

Read more

Why is an “await Task.Yield()” required for Thread.CurrentPrincipal to flow correctly?

How interesting! It appears that Thread.CurrentPrincipal is based on the logical call context, not the per-thread call context. IMO this is quite unintuitive and I’d be curious to hear why it was implemented this way. In .NET 4.5., async methods interact with the logical call context so that it will more properly flow with async …

Read more

Global exception handling in OWIN middleware

Ok, so this was easier than anticipated, thanks for @Khalid for the heads up, I have ended up creating an owin middleware named OwinExceptionHandlerMiddleware which is dedicated for handling any exception happening in any Owin Middleware (logging it and manipulating the response before returning it to the client). You need to register this middleware as …

Read more