简体   繁体   English

WebApi.Hal-HttpPost始终发送null

[英]WebApi.Hal - HttpPost always send null

I am having a problem with HttpPosts when using WebApi.Hal. 使用WebApi.Hal时,HttpPosts出现问题。 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. LoginDetails继承自Representation,但值始终为null。 Any ideas? 有任何想法吗?

Fixed, Originally I was installing the Hal formatters like this... 固定,最初我是这样安装Hal格式器的...

    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). 这样可以保留旧格式器,但在默认格式器之前插入Hal格式器(为它们提供更高的优先级)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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