ASP.NET Core – Return a redirect response

A redirect response contains a redirect status code (3xx) and a redirect URL in the Location header. The redirect URL can be an absolute or relative path. It’s up to the client to follow the redirect properly. Here’s an example of returning a redirect response using the Redirect() helper method: Note: The redirect helper methods … Read more

C# – How to read response headers with HttpClient

When you send a request with HttpClient, it returns an HttpResponseMessage. You can read the response headers through the HttpResponseMessage.Headers property: This outputs the response headers: Raw response headers are really just key/value(s) pairs. When the response comes in, the headers are loaded into the Headers property (which is of type HttpResponseHeaders). This parses the … Read more

ASP.NET Core – How to add your own action filter

Action filters allow you to look at requests right before they are routed to an action method (and responses right after they are returned from the action method). The simplest way to add your own action filter in ASP.NET Core is to subclass ActionFilterAttribute and then override the appropriate methods depending on if you want … Read more

ASP.NET Core – How to add your own middleware function

Middleware functions have access to requests before they are sent to the controllers. Likewise, they have access to responses from the controllers before they are returned to the client. This is useful for doing things like logging the request and response, generating stats about requests, handling exceptions, and many more scenarios. In this article, first … Read more