Search code examples
c#asp.netmiddleware.net-framework-versionhttp-method

How make a request in the browser to a DELETE http method in ASP.NET (.NET FRAMEWORK)


In laravel or some express.js libs add middleware to check if a field is given called _Method which will override the actual http method of the http request on arrival of the server, because the browser only supports POST and GET.

Does ASP.NET (.NET Framework) contain a build-in middleware like those I just mentioned, I couldn't find anything online.

If so, how do I use it or do you know such an atricle.
If not, how can I create such a middleware myself.
I could not create one myself, because the HttpRequest in the HttpContext is readonly and the HttpMethod in the HttpRequest is readonly aswell.

Or is the only choice to use preventDefault() and/or ajax to send (for example) a DELETE request via javascript?


Solution

  • After some digging, I found that later in the chain of ASP.NET the HttpContext Changes into HttpContextBase. This context contains GetHttpMethodOverride().
    I looked the Method up on github, because it's open source and ASP.NET does contain a middleware such as mentioned.
    You can override the actual http method using the key X-HTTP-Method-Override in either the Header, Form or Query string and the actual http method must be POST for the middleware to even consider a http method override.