400 Bad Request to Request HTTP Mail in MVC.NET API Controller

I've read a lot of tutorials but can't figure out why I'm getting the 400th Post request.

My Api Controller:

public class CategoryApiController : ApiController {

    [HttpGet]
    [ActionName("get")]
    public int GetSomething () {
        return 1;
    }

    [HttpPost]
    [ActionName("post")]
    public string PostSomething (int id) {
        return "2";
    }
}

      

My routes:

routes.MapRoute (
            "ControllerOnly",
            "api/{controller}"
            );

        routes.MapRoute (
            "ControllerAndId",
            "api/{controller}/{id}",
            new {
                id = UrlParameter.Optional
            }
        );

        routes.MapRoute (
            "ControllerAndActionAndId",
            "api/{controller}/{action}/{id}",
            new {
                id = UrlParameter.Optional,
                action = "AddSomething"
            }
        );

      

And my ajax request:

$('#click').click(function () {
    $.ajax({
        url: '/api/CategoryApi/get',
        type: 'GET',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (response) {
            $('#raspuns').text(JSON.stringify(response));
        }
    });
});

$('#raspuns').click(function () {
    $(this).text("nimic");
    $.ajax({
        url: '/api/CategoryApi/post',
        type: 'POST',
        //contentType: 'application/json; charset=utf-8',
        //dataType: 'json',
        data: {
            'id': 1
        },
        success: function (response) {
            $('#click').text(JSON.stringify(response));
        }
    });
});

      

So the GET request works fine, but the POST request returns 400 status. Explicit message from post request:

{"Message": "The request is invalid.", "MessageDetail": "The parameters dictionary contains a null entry for parameter 'id' of non-  nullable type 'System.Int32' for method 'System.String PostSomething(Int32)' in  'stackoverflow.Controllers.CategoryApiController'. An optional parameter must be a reference type, a   nullable type, or be declared as an optional parameter."}

      

The body of the request contains the ID: 1. So I sent the ID as a parameter.

The get request is sent to the first method as expected, but I don't understand why the Post request is not working.

EDIT: So I want to have complete control over which method is called in a particular controller. In JAVA, you simply specify the url above your desired method and that method will be called when the url is available. I really don't understand how to do this in .NET MVC using routes. I want to have many GET and POST methods in one controller. Can anyone give me an example or a good tutorial? PS: I read a few tutorials, but it was not what I want.

+3


source to share


1 answer


I think the problem here is that the parameter of the actual method PostSomething

is not optional. You need to either set the default or make it valid.

Examples:

public string PostSomething (int? id) {

      

or



public string PostSomething (int id = -1) {

      

Alternatively, if you want the ID to be required, you need to update the call to match the route:

$('#raspuns').click(function () {
    $(this).text("nimic");
    $.ajax({
        // Since your route is "api/{controller}/{action}/{id}", 
        // add the id to the url
        url: '/api/CategoryApi/post/1',
        type: 'POST',
        success: function (response) {
            $('#click').text(JSON.stringify(response));
        }
    });
});

      

I don't remember enough JS to make the id of a variable in the url bar.

+1


source







All Articles