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

ASP.NET MVC application can't accept DELETE requests

我在 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] 就足够了..

有什么想法吗?

我解决了替换 Web.config<system.Web> 标签内容的问题。 我改变了这个:

<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>

现在可以使用了..