问题
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