本文介绍了属性与HTTP PUT方法约束路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是新的属性与MVC5路由和已经​​得到HTTP GET POST 法的约束工作通过添加 [HTTPGET] [HttpPost] 属性我的行动方法。但是,当我加入 [HttpPut] 我只是得到一个404错误页面。有谁知道我需要做的就是属性的路由以http PUT 工作吗?请参见下面code:

  [HTTPGET]
    [路线(编辑)]
    公众的ActionResult编辑(){
        //这个工程
        返回查看();
    }    [HttpPost]
    [路线(插入)
    公众的ActionResult插入(){
        //这个工程
        返回查看();
    }    [HttpPut]
    [路线(更新)
    公众的ActionResult更新(){
        //这不起作用
        返回查看();
    }

我试着 X-HTTP-方法,覆盖= PUT

  POST /更新HTTP / 1.1
主机:本地主机:61794
内容长度:32
接受:text / html的,是application / xhtml + xml的,应用/ XML; Q = 0.9,图像/ WEBP,* / *; Q = 0.8
用户代理:Mozilla的/ 5.0(Windows NT的6.3; WOW64)为AppleWebKit / 537.36(KHTML,像壁虎)的Chrome / Safari浏览器30.0.1599.101 / 537.36
内容类型:应用程序/ x-WWW的形式urlen codeD的X HTTP的方法,覆盖= PUT和放大器;文本=

和也与一个真正的 PUT

  PUT /更新HTTP / 1.1
主机:本地主机:61794
内容长度:5
缓存控制:最大年龄= 0
接受:text / html的,是application / xhtml + xml的,应用/ XML; Q = 0.9,图像/ WEBP,* / *; Q = 0.8
用户代理:Mozilla的/ 5.0(Windows NT的6.3; WOW64)为AppleWebKit / 537.36(KHTML,像壁虎)的Chrome / Safari浏览器30.0.1599.101 / 537.36
内容类型:应用程序/ x-WWW的形式urlen codeD文=


解决方案

实时HttpPut

这应该工作,但是你必须修改ExtensionlessUrlHandler,让更多的动词:

 <&处理GT;
    <清除NAME =ExtensionlessUrlHandler - 集成 - 4.0/>
    <添加名称=ExtensionlessUrlHandler - 集成 - 4.0PATH =*。动词=*类型=System.Web.Handlers.TransferRequestHandlerpreCondition =integratedMode,runtimeVersionv4.0/>
< /处理器>

X-HTTP-方法,覆盖

这似乎是在5.0的错误(或不只是实现)。你可以尝试 MVC 5.1阿尔法,其中的的X HTTP的方法,覆盖得到尊重。

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=
解决方案

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.

这篇关于属性与HTTP PUT方法约束路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 04:38