简体   繁体   English

Web API删除方法不起作用

[英]Web API Delete Method not working

Calling a DELETE method Web API is not reaching the server. 调用DELETE方法Web API无法到达服务器。 After searching all over the web for similar issues non have worked for me. 在网上搜索了类似的问题后,对我没有帮助。 Below is a brief of my code across my solution. 以下是我的解决方案代码简介。

Web API: Web API:

    [AuthorizeService]
    [HttpDelete]
    public HttpResponseMessage Delete(Int32 id)
    {
        String username = User.Identity.Name;
        this._clientDataManager.DeleteRestaurant(id,username);

        return new HttpResponseMessage(HttpStatusCode.OK);
    }

Ajax Call: Ajax电话:

$.ajax({
    url: 'localhost:53378/api/RestaurantWebAPI/1135',
    type: "DELETE",
    statusCode: {
        200: function (data) {
            //success
        }
    }, beforeSend: setHeader
});

Web.Config: Web.Config:

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
    </modules>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
        <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
        <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
        <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
        <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.webServer>

Appreciate any help. 感谢任何帮助。

Ok got it to work but somehow the solution confused me a bit. 好的,它可以正常工作,但是解决方案使我有些困惑。 First the solution, ended up adding the below to the WebApiConfig file: 首先是解决方案,最后将以下内容添加到WebApiConfig文件中:

   config.Routes.MapHttpRoute(
       name: "DeleteApi",
       routeTemplate: "api/{controller}/Delete/{id}",
       defaults: new { name = RouteParameter.Optional }
   );

Now, isn't this a feature in MVC 4 Web API for the request to be routed directly to the delete method when the type of the request is set to "DELETE"? 现在,这不是MVC 4 Web API中的一项功能,当请求的类型设置为“ DELETE”时,该请求可以直接路由到delete方法吗? Any clarifications on this matter is highly appriciated. 对此事的任何澄清都是高度赞赏的。

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

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