使用http PUT方法约束的属性路由

时间:2013-11-04 09:54:13

标签: asp.net-mvc asp.net-mvc-5 attributerouting

我正在使用MVC5的新属性路由,并通过向我的操作添加GETPOST属性来获得http [HttpGet][HttpPost]方法限制方法。但是当我添加[HttpPut]时,我只得到一个404错误页面。有谁知道我需要做什么才能使属性路由与http PUT一起使用?请参阅以下代码:

    [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();
    }

我尝试过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=

还有一个真实的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 个答案:

答案 0 :(得分:4)

真实的HttpPut

这应该可行,但您必须修改ExtensionlessUrlHandler以允许其他动词:

<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-方法-覆盖

这似乎是5.0中的一个错误(或者不仅仅是实现)。您可以尝试nightly build MVC 5.1 Alpha,其中X-HTTP-Method-Override得到尊重。