3

我正在使用带有 MVC5 的新属性路由,并通过将and属性添加到我的操作方法中来获得 httpGET和方法约束。但是当我添加时,我只会得到一个 404 错误页面。有谁知道我需要做什么才能使属性路由与 http 一起使用?请参见下面的代码:POST[HttpGet][HttpPost][HttpPut]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=
4

1 回答 1

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-Method-Override

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

于 2013-11-06T12:21:11.883 回答