4

Calling a DELETE method Web API is not reaching the server. 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:

    [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({
    url: 'localhost:53378/api/RestaurantWebAPI/1135',
    type: "DELETE",
    statusCode: {
        200: function (data) {
            //success
        }
    }, beforeSend: setHeader
});

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ancdev
  • 207
  • 10
  • 18

1 Answers1

-5

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:

   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"? Any clarifications on this matter is highly appriciated.

ancdev
  • 207
  • 10
  • 18