Search code examples
c#asp.net-mvcasp.net-web-apiasp.net-mvc-routing

mvc call api controller PostAsJsonAsync action Method name Get


I am doing an Asp .Net MVC 5 APP and I am calling an API Controller using PostAsJsonAsync like this

int value=0;
HttpResponseMessage response = await client.PostAsJsonAsync(apiUrl + "Method/Action",value);

My Api Controller Method look like this...

[ActionName("GetByUpload")]
public int ByUpload([FromBody]long id)
{
    return 100;
}

If the Method Name starts with GET give me an error 405

Why can´t call a method started with Get?

Any way can call Get..something?


Solution

  • If you are intending to post to that action, you need to explicitly assign a HttpPost attribute to the action so that the route table knows how to match requests to that action .

    [HttPost]
    [ActionName("GetByUpload")]
    public int ByUpload([FromBody]long id)
    {
        return 100;
    }