Usually in network protocols, header of request are preprocessed and then body of request will be processed afterwards. In case of HTTP I'm not sure but I want to know are there any methods to process header before body of request and its parameters?
Talking in C# way, are there any methods to process request header before Controller method gets fired or not?
If answer is yes, I want to send client version to my server and In case they are not matching sending client suitable response. I want to do this since it might happen that body of my request be big (e.g. 10MBs) and I want to preprocess header before waiting to receive whole HTTP request.
The answer is yes, you can use the action filters. https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/filters?view=aspnetcore-3.1#action-filters
public class MySampleActionFilter : IActionFilter
{
public void OnActionExecuting (ActionExecutingContext context)
{
// Do something before the action executes.
}
public void OnActionExecuted(ActionExecutedContext context)
{
// Do something after the action executes.
}
}
As per documentation you can override the action context and short circuit the result, without going into the controller.
Result - setting Result short-circuits execution of the action method and subsequent action filters.
You can find your headers like this
var headers = context.HttpContext.Request.Headers;
// Ensure that all of your properties are present in the current Request
if(!String.IsNullOrEmpty(headers["version"])
{
// All of those properties are available, handle accordingly
// You can redirect your user based on the following line of code
context.Result = new RedirectResult(url);
}
else
{
// Those properties were not present in the header, redirect somewhere else
}