Is it possible to implement X-HTTP-Method-Override in ASP.NET MVC?

asp.net-mvc, http-headers, http-method, rest

Solution

You won't be able to use the [AcceptVerbs] attribute as-is since it's tied to the request's actual HTTP verb. Fortunately the [AcceptVerbs] attribute is very simple; you can see the source for yourself at http://www.codeplex.com/aspnet/SourceControl/changeset/view/21528#266431.

In short, subclass AcceptsVerbsAttribute and override the IsValidForRequest() method. The implementation would be something like the following:

string incomingVerb = controllerContext.HttpContext.Request.Headers["X-HTTP-Method-Override"] ?? controllerContext.HttpContext.Request.Method;
return Verbs.Contains(incomingVerb, StringComparer.OrdinalIgnoreCase);

Problem

I'm implementing a prototype of a RESTful API using ASP.NET MVC and apart from the odd bug here and there I've achieve all the requirements I set out at the start, apart from callers being able to use the `X-HTTP-Method-Override` custom header to override the HTTP method. What I'd like is that the following request... ``` GET /someresource/123 HTTP/1.1 X-HTTP-Method-Override: DELETE ``` ...would be dispatched to my controller method that implements the `DELETE` functionality rather than the `GET` functionality for that action (assuming that there are multiple methods implementing the action, and that they are marked with different `[AcceptVerbs]` attributes). So, given the following two methods, I would like the above request to be dispatched to the second one: ``` [ActionName("someresource")] [AcceptVerbs(HttpVerbs.Get)] public ActionResult GetSomeResource(int id) { /* ... */ } [ActionName("someresource")] [AcceptVerbs(HttpVerbs.Delete)] public ActionResult DeleteSomeResource(int id) { /* ... */ } ``` Does anybody know if this is possible? And how much work would it be to do so...?

Original source