How to Allow HTTP Method Override ASP Dotnet Core

Freaking easy, isn’t it. Not some times. it is in fact easier in DotnetCore. I have submitted a GitHub PR for updating the Official documentation for including HTTPOverrides Middleware.

What is it??
Good Question. So why you need it? Well there are various REST clients which don’t have capabilities to send PUT/ PATCH/ DELETE requests to your REST API. Why you would bother? because you want your REST API to be available on, if not all, then most platforms. Your clients would still send a POST call to your API, but with “X-HTTP-Method-Override:PUT” as a header, which makes your API consider the request as a PUT request.

So How to do it?  If you are using MVC 4 on ASP.NET full Framework, please refer to this nice article by Scott Hanselman Use X-HTTP-Method-Override for your REST Service with ASP.NET Web API.

However, if you want to do the same in Dotnet Core, that should be even more easier.

As of date, the documentation for Build-in Middleware,  “Forwarded Headers” is only reachable. I then found out that there is another Middleware HTTPMethodOverride, which can be used to pipe in to the Dotnet Core MVC Pipeline to convert the POST methods to PUT/ DELETE as needed.

Here is how you should do it in Startup.Configure method of your DotnetCore WebApi

public void Configure (IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    if (env.IsDevelopment ()) {
        app.UseDeveloperExceptionPage ();
    }
    app.UseHttpMethodOverride();
    app.UseMvc ();
}

Once you send a POST request via PostMan specifying correct HTTP Request Header, the request would get through like shown in snapshot below.

Postman Put as POST

EDIT 1 : You can also create your own POST Tunneling Middleware referring to this article by Tomasz Pęczekhttps://www.tpeczek.com/2017/12/post-tunneling-middleware-for-aspnet.html or you can also create your own Custom MiddleWare Refer this Official Documentation
Happy Coding !!
Like always, feel free to provide any feedback..