ASP.NET MVC应用程序无法接受DELETE请求

时间:2018-03-27 09:06:05

标签: c# ajax asp.net-mvc asp.net-ajax http-delete

我在HomeController中使用[HttpDelete]属性的简单方法:

[HttpDelete]
public void DeleteEmployee()
{
    ...
}

我正在发送Ajax这样的请求:

$.ajax({
    url: '/Home/DeleteEmployee',
    type: 'DELETE',
    contentType: "application/json;charset=utf-8",
    success: function () {
        alert('Successfully deleted');
    },
    error: function () {
        alert('Failed to delete');
    }
});

当我执行此js代码时出现404错误,只需更改type: 'GET'[HttpGet]即可。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我已将其解析为替换<system.Web>Web.config标记的内容。 我改变了这个:

<system.web>
    <compilation debug="true" targetFramework="4.6.1" />
    <httpRuntime targetFramework="4.6.1" />
    <httpModules>
        <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
    </httpModules>
</system.web>

到此:

<system.Web>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
        <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
        <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
        <remove name="ExtensionlessUrlHandler-Integrated-4.0" />

        //This will enable all Web API verbose
        <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
        <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
        <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
</system.Web>

现在正在运作..