简体   繁体   中英

WebApi.Hal - HttpPost always send null

I am having a problem with HttpPosts when using WebApi.Hal. I am posting like this:

     $.ajax({
            type: "POST",
            url: "http://localhost/TestWebApi/api/test/test2/",
            data: {
                "reference": bookingref,
                "leadname": leadpax,
                "_links": {}
            },
            contentType: "application/hal+json",
            //dataType: "application/hal+json",
            success: function(data, status) {
                alert("POST: Data: " + data + "\nStatus: " + status);
                $("#result").text(JSON.stringify(data, null, 4));
            },
            error: function(xhr, ajaxOptions, thrownError) {
                alert('POST: Status: ' + xhr.status + ', Error Thrown: ' + thrownError);
            }
        });

and picking the post up like this:

    public LoginDetails PostTest2([FromBody]LoginDetails value)
    {
        return new LoginDetails();
    }

LoginDetails inherits from Representation but value is always null. Any ideas?

Fixed, Originally I was installing the Hal formatters like this...

    GlobalConfiguration.Configuration.Formatters.Clear();
    GlobalConfiguration.Configuration.Formatters.Add(new JsonHalMediaTypeFormatter());
    GlobalConfiguration.Configuration.Formatters.Add(new XmlHalMediaTypeFormatter());

I changed it to this...

        GlobalConfiguration.Configuration.Formatters.Insert(0, new XmlHalMediaTypeFormatter());
        GlobalConfiguration.Configuration.Formatters.Insert(0, new JsonHalMediaTypeFormatter());

So that it keeps the old formatters but inserts the Hal ones before the default formatters (giving them a higher precedence).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM