Web API API Method Not Working

Calling the DELETE Web API method does not reach the server. After searching all over the internet for similar questions did not work for me. Below is a brief overview of my code in 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.

+4
source share
1 answer

Well, it worked, but for some reason it confused me a little. At first, this solution led to the addition of the WebApiConfig file below:

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

Now, is this not a feature in the MVC 4 Web API so that the request is sent directly to the delete method when the request type is set to "DELETE"? Any clarifications on this subject are greatly appreciated.

-5
source

Source: https://habr.com/ru/post/1444313/


All Articles