在 .NET MVC 上启用 Http PUT 请求 [英] Enable Http PUT requests on .NET MVC
问题描述
我正在开发一个 MVC 应用程序.在我的服务的原始草案中,我在我的一个控制器中使用了这种方法:
I'm working on an MVC application. On my original draft of the service I had this method in one of my controllers:
[AcceptVerbs(HttpVerbs.Post)]
[ActionName("UpdateRelationship")]
public ActionResult UpdateRelationship(string aParameter)
而且效果很好.在最新版本中,我被要求将其更改为 PUT 请求,以将其与使用 post 的类似添加机制区分开来.所以我改成这样:
And that worked fine. In the latest revision I've been asked to change this into a PUT request to differentiate it from a similar add mechanism which uses post. So I changed it to this:
[AcceptVerbs(HttpVerbs.Put)]
[ActionName("UpdateRelationship")]
public ActionResult UpdateRelationship(string aParameter)
突然间,我的请求收到了 404,这一切都来自于更改 AcceptVerbs.从错误的外观来看,似乎 IIS 正在尝试将请求路由为标准的 webforms 页面,而不是使用 MVC 无扩展名的 url 重写.
And suddenly I'm getting a 404 on my requests, all from just changing the AcceptVerbs. From the look of the error, it appears as though IIS is trying to route the request as a standard webforms page rather than using MVC extensionless url re-writing.
谷歌搜索这个似乎一个常见的原因是浏览器不允许 PUT 请求,但我没有使用浏览器来测试这个 - 我正在使用 Fiddler.所以那里应该没有问题.我也认为正确的设置已经在 web.config 中:
Googling this it seems that a common cause is browsers not allowing PUT requests but I'm not using a browser to test this - I'm using Fiddler. So there should be no problem there. I also think the correct settings are already in the web.config:
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule" />
</modules>
<handlers>
<remove name="UrlRoutingHandler" />
<remove name="MvcHttpHandler" />
<remove name="WebDAV" />
<add name="MvcHttpHandler" preCondition="integratedMode" verb="*" path="*.mvc" type="System.Web.Mvc.MvcHttpHandler, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</handlers>
<security>
<requestFiltering>
<verbs>
<add verb="PUT" allowed="true" />
</verbs>
</requestFiltering>
</security>
</system.webServer>
那么我错过了什么?
此代码适用于同事的机器.所以看起来我的本地 IIS 设置有问题.仍然无法解释我需要更改的内容 - 有什么想法吗?
This code works on a colleagues' machine. So it looks like my local setup of IIS as at fault. Still at a loss to explain what I need to change though - any ideas?
干杯,马特
推荐答案
经过多次涉及 WebDAV 的无用搜索和死胡同后,我在另一个 SO 家族网站上找到了答案:)
After much fruitless searching and blind alleys involving WebDAV I found the answer on another SO family site :)
https://serverfault.com/questions/93424/how-to-enable-put-and-delete-in-iis7
这篇关于在 .NET MVC 上启用 Http PUT 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!