处理POST请求后出现“方法不允许”错误

时间:2018-08-15 19:56:01

标签: c# ajax asp.net-web-api

我有一个类似主题的问题。我对这些东西不熟悉,甚至不知道在哪里寻找该问题。那是我的帖子方法:

public class Point
{
    public string x { get; set; }
    public string y { get; set; }
}

[HttpPost]
public IHttpActionResult Post([FromBody] Point point)
{
    // do stuff
    return StatusCode(HttpStatusCode.OK);
}

那是我的ajax请求:

var x = $('#input1').val();
var y = $('#input2').val();

$("#btnUpload").click(function () {
    $.ajax({
        type: "POST",
        url: "api/images",
        data: { x: x, y: y },
        success: function (result) { alert(result) },
        error: function (err) { alert(err.statusText) }
    });
});

我忘了什么吗?

编辑:我的控制器:

[RoutePrefix("api/images")]
public class ImagesController : ApiController
{
    private Bitmap CreateBoard()
    {
        // some stuff I need
    }

    [HttpPost]
    public IHttpActionResult Post([FromBody] Point point)
    {
        // do stuff
        return StatusCode(HttpStatusCode.OK);
    }

    [Route("")]
    [HttpGet]
    public List<int> Get()
    {
        // do other stuff
    }

    [Route("{id}")]
    [HttpGet]
    public HttpResponseMessage Get(int id)
    {
        // do other other stuff
    }
}

GET运行正常。

1 个答案:

答案 0 :(得分:0)

您的网址应为Function LookBehindRegex( _ ByVal inputText As String, nonCaptureRegex As String, _ captureRegex As String) As String Dim regEx As New RegExp Dim mac As MatchCollection regEx.IgnoreCase = True regEx.Pattern = "(" & nonCaptureRegex & ")(" & captureRegex & ")" Set mac = regEx.Execute(inputText) If mac.Count > 0 Then _ LookBehindRegex = mac(0).SubMatches(1) End Function api/images/post是“ api / images”,因此您缺少方法的名称。