简体   繁体   English

如何强制JSON请求正文在ASP.NET Web API 2中包含某些特定参数

[英]How to force JSON request body to contain some specific parameter in ASP.NET Web API 2

In our project, there is a POST method which take a JSON request body like this: 在我们的项目中,有一个POST方法,它采用这样的JSON请求正文:

{
"UserId": string,
"Username": string,
"Email": string
}

It's ok if "Email" is null but we want it to always present in the request body. 可以将“ Email”为空,但我们希望它始终出现在请求正文中。

So this is OK: 这样就可以了:

{
"UserId": "u12324",
"Username": "tmpUser",
"Email": null
}

but this is not: 但这不是:

{
"UserId": "u12324",
"Username": "tmpUser"
}

Do you have any ideas? 你有什么想法? Is it even possible? 可能吗?

You are using , which uses as its underlying JSON serializer, according to JSON and XML Serialization in ASP.NET Web API . 根据ASP.NET Web API中的JSON和XML序列化,您正在使用 ,它将用作其底层JSON序列化器 This serializer allows you to specify that a given property must be present (and optionally non-null) with the JsonPropertyAttribute.Required attribute setting, which has 4 values : 该序列化程序允许您使用JsonPropertyAttribute.Required属性设置指定给定的属性必须存在(并且可以选择为非null),该属性设置具有4个值

 Default The property is not required. The default state. AllowNull The property must be defined in JSON but can be a null value. Always The property must be defined in JSON and cannot be a null value. DisallowNull The property is not required but it cannot be a null value. 

The following class makes use of these attributes: 下面的类使用这些属性:

public class EmailData
{
    [JsonProperty(Required = Required.Always)] // Must be present and non-null
    public string UserId { get; set; }

    [JsonProperty(Required = Required.Always)] // Must be present and non-null
    public string Username { get; set; }

    [JsonProperty(Required = Required.AllowNull)] // Must be present but can be null
    public string Email { get; set; }
}

Note that setting [JsonProperty(Required = Required.Always)] will cause an exception to be thrown during serialization if the property value is null. 请注意,如果属性值为null,则设置[JsonProperty(Required = Required.Always)]将导致在序列化期间引发异常。

Try to pass all parameters in a object 尝试传递对象中的所有参数

Example

[HttpPost]
        public bool Create(DoSomething model)
        {
            return true
        }



public class DoSomething 
    {
        public int UserId{ get; set; }
        public string UserName{ get; set; }
        public string Email{ get; set; }
    }

暂无
暂无

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

相关问题 如何知道RESTful请求在ASP.Net Web API 2中是否不包含某些参数? - How to know if RESTful request didn't contain some arguments in ASP.Net Web API 2? Why Fetch Post request with JSON parameter in the body to ASP.NET Core 3.1 WEB API or MVC Controller does not get anything? - Why Fetch Post request with JSON parameter in the body to ASP.NET Core 3.1 WEB API or MVC Controller does not get anything? ASP.NET WEB API-CamelCasePropertyNamesContractResolver-强制忽略特定端点 - ASP.NET WEB API - CamelCasePropertyNamesContractResolver - force to ignore specific endpoints 在ASP.NET Web Api中获取请求正文 - Get body of request in ASP.NET Web Api ASP.NET Web API:DateTime绑定在json主体中不起作用 - ASP.NET Web API: DateTime binding not working in json body ASP.NET Web Api 2-基于属性的路由-如何强制参数仅查询字符串? - ASP.NET Web Api 2 - Attribute Based Routing - How do I force a parameter to query string only? 如何将ASP.NET Web API请求正文记录到Application Insights故障中? - How to log ASP.NET Web API request body into a Application Insights failure? 如何在 ASP.NET Core 5 Web API 中绑定来自请求主体的简单类型 - How can I bind simple type coming from body of the request in ASP.NET Core 5 Web API 如何在生产环境中禁用 Asp.Net Core web API 中的 400 Bad request 的消息正文? - how to disable 400 Bad request's message body in Asp.Net Core web API in production environment? 如何将 POST 请求正文中的有效 XML 提交到使用 XML 的 ASP.NET Core Web API? - How to submit valid XML in a POST request body to an ASP.NET Core Web API that consumes XML?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM