简体   繁体   English

Web API 2 POST请求返回null

[英]Web API 2 POST-request return null

The problem is, when the method is entered, "session" is a null. 问题是,当输入方法时,“session”为空。 As you can see, headers and JSON-object are correct. 如您所见,标头和​​JSON对象是正确的。 Could you tell me what I'm doing wrong? 你能告诉我我做错了什么吗?

My POST-request with angular: 我有角度的POST请求:

        $scope.send = function () {
        var data = { "session": session };
        var req = {
            method: 'POST',
            data: data,
            headers: {'Content-Type': 'application/json'},
            url: '/api/myquiz/answers/'
        }

        $http(req).success(
            function () {
                quiz();
            });
    }

and C# code: 和C#代码:

    [Route("answers")]
    [HttpPost]
    public IActionResult AnswerPost([FromBody] string session) {

       ...

    }

Chrome console: Chrome控制台:

 Request URL:http://localhost:39634/api/myquiz/answers/ Request Headers Provisional headers are shown Accept:application/json, text/plain, */* Content-Type:application/json Origin:http://localhost:39634 Referer:http://localhost:39634/Team/Index User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.93 Safari/537.36 Request Payload {session: "92dfb7e432224702a553c98c294b29cf"} session: "92dfb7e432224702a553c98c294b29cf" 

Per Web API documentation , try directly sending session (raw json string) instead of putting it in a dictinary (json object). 根据Web API 文档 ,尝试直接发送会话(原始json字符串),而不是将其放在一个dictinary(json对象)中。

var req = {
    method: 'POST',
    data: '"' + session + '"',
    headers: {'Content-Type': 'application/json'},
    url: '/api/myquiz/answers/'
}

You are posting an object to WebApi: 您正在向WebApi发布对象:

var data = { "session": session };

With this you are saying that you are passing an object that has a session property on it. 有了这个,你说你传递的是一个具有会话属性的对象。 What you are telling your WebApi method to expect though is a string: 你告诉你的WebApi方法所期望的是一个字符串:

public IActionResult AnswerPost([FromBody] string session)

not an object. 不是一个对象。 I think if you change your code to this: 我想如果你改变你的代码:

$scope.send = function () {
        //var data = { "session": session };
        var req = {
            method: 'POST',
            data: session,
            headers: {'Content-Type': 'application/json'},
            url: '/api/myquiz/answers/'
        }

        $http(req).success(
            function () {
                quiz();
            });
    }

It should work. 它应该工作。 Hope this helps. 希望这可以帮助。 If you are going to pass more params to the function in the future though, I would create a C# object to pass to the controller. 如果你将来要将更多的参数传递给函数,我会创建一个C#对象传递给控制器​​。 like this: 像这样:

public class AnswerPost
{
  public string Session {get; set;}
}

and then update the controller method to look like this: 然后更新控制器方法,如下所示:

[Route("answers")]
    [HttpPost]
    public IActionResult AnswerPost([FromBody] AnswerPost answerpost) {
...
}

Web API actions require a value matching the parameter type to be able to deserialize the body. Web API操作需要与参数类型匹配的值才能反序列化正文。

So, if your Web API parameter is a value type, like your string session , you have to post a single value, ie something like 'my string' , without extra wrappers. 因此,如果您的Web API参数是一个值类型,就像您的string session ,您必须发布一个值,即'my string' ,而不需要额外的包装器。

If your Web API parameter was a complex one, for example a class with properties, then you would have to post a JSON object, like you do in your question. 如果您的Web API参数是一个复杂的参数,例如一个具有属性的类,那么您必须发布一个JSON对象,就像您在问题中所做的那样。

Ie your parameter should be an object of a class like this: 即你的参数应该是这样一个类的对象:

public class ParamClass
{
   public string session { get; set; } // Note: it's case.sensitive
}

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

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