简体   繁体   English

ASP.NET WebAPI在帖子体中的空值?

[英]ASP.NET WebAPI Null value in post body?

I have just started learning WebAPI today and I can't figure out why "account" is always null. 我今天刚刚开始学习WebAPI,我无法弄清楚为什么“帐户”始终为空。

Request 请求

Content-Type: application/json; charset=utf-8
Request-Body: {"account":{"email":"awd","password":"awad","isNewsletterSubscribed":false}}

WebAPI 的WebAPI

 public class AccountsController : ApiController
    {
        public void Post([FromBody] string account)
            {
                    // account is null
            }
    }

Shouldn't account contain a json string in this case? 在这种情况下,帐户不应该包含json字符串吗?

Shouldn't account contain a json string in this case? 在这种情况下,帐户不应该包含json字符串吗?

That would depend on the specific Content-Type request header you set when you send the request. 这取决于您发送请求时设置的特定Content-Type请求标头。 For example if you used application/x-www-form-urlencoded which is the default then your request body payload must have looked like this: 例如,如果您使用默认的application/x-www-form-urlencoded ,则您的请求正文有效负载必须如下所示:

={"account":{"email":"awd","password":"awad","isNewsletterSubscribed":false}}

Notice the = character at the beginning. 注意开头的=字符。 That's one of the biggest weirdest things I have ever encountered. 这是我遇到过的最奇怪的事情之一。 Since you can bind only one parameter from the body if the request the Web API doesn't expect a parameter name, but just the value. 因为如果请求Web API不期望参数名称,而只是绑定值,则只能从主体绑定一个参数。

This being said, your request payload looks more like a JSON. 话虽如此,您的请求有效负载看起来更像是JSON。 So it would make far more sense to design a view model and use Content-Type: application/json when sending the request. 因此,设计视图模型并在发送请求时使用Content-Type: application/json会更有意义。 Binding a JSON object to a string is not common practice. 将JSON对象绑定到字符串不常见。

So: 所以:

public class UserViewModel
{
    public string Email { get; set; }
    public string Password { get; set; }
    public bool IsNewsletterSubscribed { get; set; }
}

public class AccountViewModel
{
    public UserViewModel Account { get; set; }
}

and then your controller action will simply take the view model as parameter. 然后您的控制器操作将简单地将视图模型作为参数。 In this case yo udon't need to decorate it with the [FromBody] attribute because by convention in Web API the default model binder will attempt to bind complex types from the body of the request: 在这种情况下,您不需要使用[FromBody]属性来装饰它,因为按照Web API中的约定,默认模型绑定器将尝试从请求主体绑定复杂类型:

public class AccountsController : ApiController
{
    public HttpResponseMessage Post(AccountViewModel model)
    {
        // work with the model here and return some response
        return Request.CreateResponse(HttpStatusCode.OK);
    }
}

Also notice that since HTTP is a request/response protocol it makes much more sense to have your Web API controller actions return response messages as shown in my example rather than just having some void methods. 另请注意,由于HTTP是一个请求/响应协议,因此让Web API控制器操作返回响应消息更有意义,如我的示例所示,而不仅仅是使用一些void方法。 This makes the code more readable. 这使代码更具可读性。 You immediately understand how the server will respond and with what status code to the specified request. 您可以立即了解服务器将如何响应以及指定请求的状态代码。

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

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