C# 如何在 IIS 中为 ASP.NET MVC 启用 HTTP PUT 和 DELETE?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12440277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:17:30  来源:igfitidea点击:

How do I enable HTTP PUT and DELETE for ASP.NET MVC in IIS?

c#asp.net-mvc-3iis-7.5http-puthttp-delete

提问by agent47

I use HTTP PUTand DELETEin my ASP.NET MVC3 application. When I run it in local, every thing works correctly; But when I publish the application to the server, these methods do not work.

我使用HTTPPUTDELETE我的ASP.NET MVC3应用。当我在本地运行它时,一切正常;但是当我将应用程序发布到服务器时,这些方法不起作用。

Are there any special settings for enable a web server to support PUTand DELETErequests? I'm using shared hosting with IIS 7.5.

是否有任何特殊设置可以使 Web 服务器支持PUTDELETE请求?我在 IIS 7.5 中使用共享主机。

UPDATE:

更新:

I enable PUTand DELETErequests in IIS manager. PUTcommand work fine. But DELETEstill not works. I create requests by jQuery:

我能PUTDELETE在请求IIS managerPUT命令工作正常。但DELETE仍然不起作用。我通过jQuery以下方式创建请求:

I'm in this page:

我在这个页面:

http://domain.com/dashboard/edit-site/103323/links/

and my ajax call is:

我的ajax调用是:

$.ajax({
    // url: same as page-url,
    cache: false,
    type: 'DELETE',
    data: { linkid: $(link).data("linkid") },
    beforeSend: function () {
        // doing something in UI
    },
    complete: function () {
        // doing something in UI
    },
    success: function (data) {
        // doing something in UI
    },
    error: function () {
        // doing something in UI
    }
});

This will create a request like this:

这将创建一个这样的请求:

Accept: */*
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Origin: http://domain.com
Referer: http://domain.com/dashboard/edit-site/103323/links/
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1
X-Requested-With: XMLHttpRequest

With this Form Data:

有了这个Form Data

linkid:104044

回答by danielQ

Go to Handler Mappingsin your IIS Manager. Find ExtensionlessUrlHandler-Integrated-4.0, double click it. Click Request Restrictions...button and on Verbs tab, add both DELETEand PUT. enter image description here

进入处理程序映射IIS管理器。找到ExtensionlessUrlHandler-Integrated-4.0,双击它。单击Request Restrictions...按钮,然后在 Verbs 选项卡上,添加DELETEPUT在此处输入图片说明



EDIT: Possible WebDav Publisher issue

编辑:可能的 WebDav 发布者问题

You've mention on a deleted post you were running on a 2008 server right? Try removing webDavrole, or disable it from your site config: on system.webServer-> modulessection, remove WebDAVModulemodule:

您在已删除的帖子中提到过您在 2008 年服务器上运行,对吗?尝试删除webDav角色,或从您的站点中禁用它config:在system.webServer->modules部分,删除WebDAVModule模块:

<system.webServer>
  <modules>
    <remove name="WebDAVModule" />
  </modules>
  <handlers>
    <remove name="WebDAV" />
  </handlers>
</system.webServer>

回答by agent47

Finally I find the answer fluky. I changed the jQuery call to tho below and it's working well now.

最后我发现答案很不稳定。我将 jQuery 调用更改为下面的 tho 并且现在运行良好。

$.ajax({ 
    url: this.href + "?linkid=" + $(link).data("linkid"), 
    cache: false, 
    type: 'DELETE', 
    // data: { linkid: $(link).data("linkid") }, 
    beforeSend: function () { 
        // doing something in UI 
    }, 
    complete: function () { 
        // doing something in UI 
    }, 
    success: function (data) { 
        // doing something in UI 
    }, 
    error: function () { 
        // doing something in UI 
    } 
});

Do you have any explanation why a DELETEcall, can't have Form Data? While on local it had and worked fine?

你有什么解释为什么一个DELETE电话,不能有Form Data?在本地它有并且工作正常吗?

回答by Heera Jaiswal

If you are getting following error in your production environment in the asp.net web api on PUTor DELETEthough these methods are working fine locally.

如果您在生产环境中的 asp.net web api 中遇到以下错误,PUT或者DELETE这些方法在本地运行良好。

405 - http verb used to access this page is not allowed.

405 - 不允许用于访问此页面的 http 动词。

Just add following settings in your server's web.config

只需在服务器的 web.config 中添加以下设置

<system.webServer>
    <handlers>
      <remove name="WebDAV" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
    <modules>
        <remove name="WebDAVModule" />
    </modules>
</system.webServer>

Cause: webDAVmodule blocks PUT/DELETE methods by default. So first remove this module and its handler. We first remove any existing ExtensionlessUrlHandler-Integrated-4.0 settings and add it with desired pathand verbs.

原因:webDAV模块默认阻止 PUT/DELETE 方法。所以首先删除这个模块及其处理程序。我们首先删除任何现有的 ExtensionlessUrlHandler-Integrated-4.0 设置并添加所需的pathverbs

回答by Adeel Asghar

You just need to add the following lines of code in your web.config

您只需要在 web.config 中添加以下代码行

<system.webServer>
 <security>
    <requestFiltering>
        <verbs allowUnlisted="false">
            <add verb="GET" allowed="true" />
            <add verb="POST" allowed="true" />
            <add verb="DELETE" allowed="true" />
            <add verb="PUT" allowed="true" />
        </verbs>
    </requestFiltering>
</security>

AND

<modules>
    <remove name="WebDAVModule" />
</modules>
<handlers>
    <remove name="WebDAV" />
</handlers>