How to resolve the "404 error" that occurs when delete API is called in WebAPI application?
Problem
While triggering the deleteAPI in the WebApi sample, the following error is obtained and thus the deleteApi is not called in ASP.NET MVC Grid.
Server Error in '/' Application.
The resource cannot be found.
Description
HTTP 404. The resource you are looking for, or one of its dependencies, could have been removed, had its name changed, or is temporarily unavailable. Review the following URL and make sure that it is spelled correctly.
Requested URL: /api/Orders/10248
Reason
When the POST and PUT actions are triggered, no additional parameters are appended to the Route link, that is api/Orders, and so these events are triggered. Refer to the following screenshot.
Figure 1: POST and PUT actions triggered
When the DELETE action is triggered, the primary key value is also appended to the Route link, that is, api/Orders/10248. Refer to the following screenshot.
Figure 2: DELETE action triggered
Solution
Since the Route in the WebApiConfig file does not include the id parameter, the 404 error is thrown. Refer to the following screenshot.
Incorrect Example
public static void Register(HttpConfiguration configuration) { configuration.Routes.MapHttpRoute("API Default", "api/{controller}", new { id = RouteParameter.Optional }); }
So changing the Route link in the WebApiConfig resolves this error.
Correct Example
public static void Register(HttpConfiguration configuration) { configuration.Routes.MapHttpRoute("API Default", "api/{controller}/{id}", new { id = RouteParameter.Optional }); }
Result:
After changing the route in the WebApiConfig.cs file, the deleteApi is called as shown in the following screenshot.
Figure 3: Result
Conclusion
I hope you enjoyed learning about how to resolve the "404 error" that occurs when delete API is called in WebAPI application.
You can refer to our ASP.NET MVC Grid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET MVC Grid example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!