I am using WebApi in a ASP.Net web application. I have a method in the controller called Delete
and I want to access to this method by using jQuery's AJAX method.
Below is my code:
[Authorize]
public int Delete(int proposalId)
{
// logic here...
}
$.ajax({
url: "/Controller/Proposal/" + proposalId,
type: "Post",
contentType: "application/json",
success: function() {
bootbox.alert("Proposal deleted successfully.");
ReloadGrid();
},
error: function() {
}
});
RouteTable.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "controller/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"></modules>
</system.webServer>
The problem is that when I am using POST
it is executing another method which starts as Post. Can anyone please help me with this?
Assuming this is accessing a REST API, you need to send a DELETE
request by setting the appropriate type
in your $.ajax
call:
$.ajax({
url: "/Controller/Proposal/" + proposalId,
type: "DELETE", // <- Change here
contentType: "application/json",
success: function() {
bootbox.alert("Proposal deleted successfully.");
ReloadGrid();
},
error: function() {
}
});