Attribute routing with http PUT method constraint

99封情书 提交于 2019-12-09 13:42:06

问题


I'm using the new attribute routing with MVC5 and have gotten http GET and POST method constraints to work by adding the [HttpGet] and [HttpPost] attributes to my action methods. But when I add [HttpPut] I just get a 404 error page. Does anyone know what I need to do to get attribute routing working with http PUT? See code below:

    [HttpGet]
    [Route("edit")]
    public ActionResult Edit() {
        // this works
        return View();
    }

    [HttpPost]
    [Route("insert")]
    public ActionResult Insert() {
        // this works
        return View();
    }

    [HttpPut]
    [Route("update")]
    public ActionResult Update() {
        // this does not work
        return View();
    }

I've tried with X-HTTP-Method-Override=PUT

POST /update HTTP/1.1
Host: localhost:61794
Content-Length: 32
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Content-Type: application/x-www-form-urlencoded

X-HTTP-Method-Override=PUT&text=

And also with a real PUT

PUT /update HTTP/1.1
Host: localhost:61794
Content-Length: 5
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Content-Type: application/x-www-form-urlencoded

text=

回答1:


Real HttpPut

This should work, but you have to modify the ExtensionlessUrlHandler to allow additional verbs:

<handlers>
    <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
    <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>

X-HTTP-Method-Override

This seems to be a bug (or not just implemented) in 5.0. You could try a nightly build of MVC 5.1 Alpha, where X-HTTP-Method-Override gets respected.



来源:https://stackoverflow.com/questions/19765188/attribute-routing-with-http-put-method-constraint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!