简体   繁体   English

在Asp.net Mvc 5中的HttpPut?

[英]HttpPut in Asp.net Mvc 5?

I was trying to use "PUT" in my controller like 我试图在我的控制器中使用“PUT”

public class HomeController : Controller
    {
        [HttpPut]
        public ContentResult Test()
        {
            return Content("hi");
        }
    }

but I can't access it. 但我无法访问它。 I thought it was my code but after trying fiddler and getting the same error(404). 我认为这是我的代码,但在尝试了提琴手并得到同样的错误后(404)。

I think something else is up. 我认为还有别的东西了。 I am just using iis express and mvc 5. 我只是使用iis express和mvc 5。

Before I never used HttpPut but been doing webapi lately so that's why I tried to do it in mvc 5 controller but I am not sure if in this case it bring me anything(especially with extra setup is needed as it seems like it is need) 之前我从未使用HttpPut但最近一直在做webapi所以这就是为什么我试着在mvc 5控制器中这样做但我不确定在这种情况下它是否带给我任何东西(特别是需要额外的设置,因为它似乎需要)

在此输入图像描述

Post request works 发布请求有效

在此输入图像描述

Another possibility is to update the ExtensionlessUrlHandler-Integrated-4.0. 另一种可能性是更新ExtensionlessUrlHandler-Integrated-4.0。

<system.webServer>
    <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>
</system.webServer>

This solution can be found on: Attribute routing with http PUT method constraint 可以在以下位置找到此解决方案: 使用http PUT方法约束的属性路由

There are several possibilities for this failure. 这种失败有几种可能性。 One is security, meaning that PUT header is being blocked: 一个是安全性,意味着PUT标头被阻止:

Does your web server allow PUT headers? 您的Web服务器是否允许PUT标头? Try this: 尝试这个:

...    
 <security>
            <requestFiltering>
                <verbs>
                    <add verb="PUT" allowed="true" />
                </verbs>
            </requestFiltering>
        </security>
    </system.webServer>

The other possibility is that your test is being subject to CORS (Cross Origin Resource Sharing) and you may need to enable it in MVC. 另一种可能性是您的测试受CORS(跨源资源共享)的约束,您可能需要在MVC中启用它。 I have personally not done this MVC but only WebApi. 我个人没有做过这个MVC,只有WebApi。 I hope this link can help you: 我希望这个链接可以帮助你:

http://www.c-sharpcorner.com/Blogs/11609/how-to-enable-cross-origin-resource-sharing-in-mvc.aspx http://www.c-sharpcorner.com/Blogs/11609/how-to-enable-cross-origin-resource-sharing-in-mvc.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM