5

我在 ASP.Net Web 应用程序中使用 WebApi。我在控制器中有一个名为的方法Delete,我想通过使用 jQuery 的 AJAX 方法来访问此方法。下面是我的代码:

[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>

问题是,当我使用POST它时,它正在执行另一种以 Post 开头的方法。谁能帮我解决这个问题?

4

2 回答 2

7

假设这是访问 REST API,您需要通过在调用中DELETE设置适当的来发送请求:type$.ajax

$.ajax({
    url: "/Controller/Proposal/" + proposalId,
    type: "DELETE", // <- Change here
    contentType: "application/json",
    success: function() {
        bootbox.alert("Proposal deleted successfully.");
        ReloadGrid();
    },
    error: function() {
    }
});
于 2015-03-18T13:52:52.760 回答
-1

例如,如果您必须使用自定义方法进行删除,您始终可以依赖设置 HTTP 属性——如下所示:

[HttpDelete]
[Route("api/Proposal")]
public IHttpActionResult Proposal(long id){
   ...
}
于 2017-10-24T17:51:43.477 回答