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

后端 未结 8 1751
遥遥无期
遥遥无期 2020-12-18 08:02

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 f

8条回答
  •  天涯浪人
    2020-12-18 08:39

    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);
    

提交回复
热议问题