0

我想要一个ApiController带有控制器动作的控制器动作,它接受 POST 请求并且名称以Delete. 我正在使用以下 jQuery:

function DeleteMyDomainObjectButton_OnClick() {
    var $div = $(this).closest("div.domain-object");
    var url = baseUrl + "Api/MyBusiness/DeleteMyDomainObject";
    var dto = {
        "MyDomainObjectId": $div.find("input[name=MyDomainObjectId]").val()
    };

    $.ajax({
        complete: DeleteMyDomainObjectAjax_OnComplete
      , dataType: "json"
      , contentType: "application/json"
      , accept: "applcation/json"
      , data: JSON.stringify(dto)
      , error: DeleteMyDomainObjectAjax_OnError
      , method: "POST"
      , success: DeleteMyDomainObjectAjax_OnSuccess
      , url: url
    });
}

我不断收到 405: {"Message":"The requested resource does not support http method 'POST'."}。响应头是:

HTTP/1.1 405 Method Not Allowed
Cache-Control: no-cache
Pragma: no-cache
Allow: DELETE
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Tue, 07 Jul 2015 20:45:35 GMT
Content-Length: 73

我确实检查了请求标头,它实际上正在发送一个 POST。这是我的控制器:

public class BaseApiController : ApiController
{
    /* Properties for All API Controllers set by DI */
}

public class MyBusinessController : BaseApiController
{
    public virtual MyDomainObject FooBar(DeleteMyDomainObjectDto dto)
    {
        return this.DeleteMyDomainObject(p_input);
    }

    [HttpPost] // Doesn't work.
    // [AcceptVerbs(HttpVerbs.Post)] // Also doesn't work.
    public virtual MyDomainObject DeleteMyDomainObject(DeleteMyDomainObjectDto dto)
    {
        return BusinessLogic.DeleteMyDomainObject(dto)
    }
}

现在,如果我将我引用的控制器操作从 jQuery 更改为FooBar,它可以工作。

问题:我怎样才能关闭ApiController只允许对名称以字符串开头的控制器操作的 DELETE 请求的魔法酱Delete?还是我做错了什么?

4

0 回答 0