简体   繁体   English

如何知道RESTful请求在ASP.Net Web API 2中是否不包含某些参数?

[英]How to know if RESTful request didn't contain some arguments in ASP.Net Web API 2?

I want my client to be able to sent some data of the model to the web via Restful request. 我希望我的客户能够通过Restful请求将一些模型数据发送到Web。 How can I know if not all part of the object is sent? 我怎么知道不是对象的所有部分都已发送?

For example, the request sent only id, datetime_updated but not qty and datetime_updated. 例如,请求仅发送id,datetime_updated,但不发送qty和datetime_updated。

I know that the framework will set the value to the default of its type, so I can check if DateTime wasn't sent (the default value is 1/1/0001 12:00:00AM which have no meaning in my application). 我知道该框架会将值设置为其类型的默认值,因此我可以检查是否未发送DateTime (默认值为1/1/0001 12:00:00 AM,在我的应用程序中没有任何意义)。 But what about int , I can't simply check that if the value is 0 (0 has it meaning). 但是int ,我不能简单地检查值是否为0(0表示含义)。

// Model
namespace TestingAPI.Models
{
    public class Operator
    {
        public int pkey { get; set; }
        public string id { get; set; }
        public int qty {get; set;}
        public DateTime datetime_created { get; set; }
        public DateTime datetime_updated { get; set; }
    }
}
// Controller
namespace TestingApi.Controllers
{
    public class ProcessingPalletController : ApiController
    {
        public Dictionary<string, object> post(Product dataIn)
        {
            // how can I know if not all argument in Product is sent?
        }

    }
}

You can use attribute over your member of model and check Weather you model is valid or not. 您可以在模型成员上使用属性,并检查模型的天气是否有效。
Like if you want to make any property require you can use [Required] attribute over you model field. 就像您要设置任何属性一样,可以在模型字段上使用[Required]属性。

public class Operator
{
    [Required]
    public int pkey { get; set; }
    public string id { get; set; }
    public int qty {get; set;}
    public DateTime datetime_created { get; set; }
    public DateTime datetime_updated { get; set; }
}

and inside your 在你里面

public Dictionary<string, object> post(Product dataIn)
{
    if (ModelState.IsValid)
    {
    }

check model is valid or not as above. 检查模型是否有效(如上所述)。 You can get the error count etc for each model field (property). 您可以获取每个模型字段(属性)的错误计数等。

You can also create your own attribute and use them. 您也可以创建自己的属性并使用它们。 Few examples of custom attribute 定制属性的几个例子

  1. http://www.codeproject.com/Articles/301022/Creating-Custom-Validation-Attribute-in-MVC http://www.codeproject.com/Articles/301022/Creating-Custom-Validation-Attribute-in-MVC

You can make the optional members nullable: 您可以使可选成员为空:

namespace TestingAPI.Models
{
    public class Operator
    {
        public int pkey { get; set; }
        public string id { get; set; }
        public int? qty {get; set;}
        public DateTime datetime_created { get; set; }
        public DateTime? datetime_updated { get; set; }
    }
}

When the consumer doesn't provide them, the properties will be null. 当使用者不提供它们时,这些属性将为null。

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

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