0

I have an asynchronous task set up in a C# controller like so that has been simplified here:

namespace MyApp.Api {

public class TimeAllocationController : BaseApiController
{
    [Route("api/timeallocation")]
    [HttpPost]
    public async Task<ActivityValidationResult> Post(string id)
    {
        //logic here...
    }

Ideally I'd like to pass the whole payload in JQuery using the $.post() method, but I keep getting a 405 method not allowed if

I try to pass in C#'s Post() string Id within the payload. I can only pass it in like so:

$.post('/api/timeallocation/' + categoryId...

I cannot pass it in like so:

$.post('/api/timeallocation?id=' + categoryId...

I'd like to do neither of the above, just set up a payload variable in the JS file with the id and all the other required attributes, and call $.post() on that.

As for other possible reasons for the 405 error, I have verified that it is not due to authentication reasons.

Is there something I am overlooking here?

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Nubtacular
  • 1,367
  • 2
  • 18
  • 38
  • You need to change the route configuration to `[Route("api/timeallocation/{id}")]` and do `$.post('/api/timeallocation/' + categoryId` to call from Ajax – Chetan Sep 11 '18 at 00:15

4 Answers4

1

If you want to call it with a payload from jquery you should have your Post method with the [FromBody] attribute like this:

public class TimeAllocationController : BaseApiController
{
    [Route("api/timeallocation")]
    [HttpPost]
    public async Task<ActivityValidationResult> Post([FromBody] string id)
    {
        //logic here...
    }

See the documentation

And then you can call it with

$.ajax({
    url: "/api/timeallocation/",
    dataType: "json",
    type: "POST",
    data: {
        id: categoryId
    }
});
emmanuel
  • 765
  • 8
  • 15
  • I was unware of `[FromBody]`. Whether it be your answer or others posted here, it seems the only way I can NOT get a 405method error is if I do `Post(string id`) in the controller and in the JS have the route be `$.post('api/timeallocation/' + categoryId...` . Any use of `FromUri`, `FromBody`, query string (ie, `?id=categoryId`) always returns a 405. What am I overlooking here? – Nubtacular Sep 11 '18 at 20:48
  • Can you share the complete code of how you are doing the post? You mentioned in your question that you're passing multiple attributes so maybe this answer is of help: https://stackoverflow.com/a/37518529/95499. Basically doing `data: JSON.stringify(payload),` – emmanuel Sep 11 '18 at 21:10
  • Other thing to consider, but I don't know since it's not in the code snippet you provided; have you enabled CORS? https://stackoverflow.com/q/26649361/95499 – emmanuel Sep 11 '18 at 21:16
0

You should call

$.post('/api/timeallocation/', categoryId)

Or you can add [FromUri] attribute of id parameter and call

$.post('/api/timeallocation?id' + categoryId)
Othman Fayed
  • 135
  • 7
0
public class TimeAllocationController : BaseApiController
{
    [Route("api/timeallocation")]
    [HttpPost]
    public async Task<ActivityValidationResult> Post(JObject json)
    {
        string id = json["id"];
        //login here
    }
}

or

public class TimeAllocationController : BaseApiController
{
    [Route("api/timeallocation")]
    [HttpPost]
    public async Task<ActivityValidationResult> Post(dynamic json)
    {
        string id = json.id ?? "";
        //login here
    }
}

$.ajax({
    url: "/api/timeallocation/",
    dataType: "json",
    type: "POST",
    data: {
        id: categoryId
    }
});
0

I was able to solve it with the following in my controller:

public async Task<ActivityValidationResult> Post(string id, [FromBody] TimeAllocationActivity payload)

where payload handles the TimeAllocationActivity attributes. Note I did have to create the TimeAllocationActivity model as it did not exist before.

On the JS side I created the payload variable and then set up the request like so:

processCheckAjax = $.ajax({
    url: "/api/timeallocation/" + categoryId,
    dataType: "json",
    type: "POST",
    data: JSON.stringify(payload)

I do find it odd that I still had to append categoryId to the route and it could not be included in the payload.

Nubtacular
  • 1,367
  • 2
  • 18
  • 38