[英]Model State Validation in ASP.NET WEBAPI Core
I am trying to use model validation in ASP.NET WEBAPI Core. 我试图在ASP.NET WEBAPI Core中使用模型验证。 Below mentioned is the code from my model.
下面提到的是我的模型中的代码。
[Range(typeof(decimal), "1.0", "90.1")]
public decimal price{ get; set; }
My understanding is, if I pass any value which is not in between 1.0 - 90.1, the ModelState.Valid
should be false . 我的理解是,如果我传递的值不在1.0 - 90.1之间,则
ModelState.Valid
应为false 。
Here is the url I tried to call the method, 这是我试图调用该方法的网址,
http://localhost:57270/api/testprice?price=132.7492634
Since the value which I have passed greater than 90, I was expecting ModelState.Valid
as false . 由于我传递的值大于90,我期望
ModelState.Valid
为false 。 But always the ModelState.Valid
is coming as true . 但是,
ModelState.Valid
总是如此 。
Am I missing anything? 我错过了什么吗? Please help?
请帮忙?
Try this: 试试这个:
[Range(1.0, 90.1)]
public decimal price{ get; set; }
For the benefit of other users, I am posting the answer to this question. 为了其他用户的利益,我发布了这个问题的答案。
To get full functionality of data annotations in .net core, we need to add the below line in ConfigureServices function in startup.cs file: 要获得.net核心中数据注释的全部功能,我们需要在startup.cs文件中的ConfigureServices函数中添加以下行:
services.AddMvcCore().AddDataAnnotations();
Hope it helps. 希望能帮助到你。 Have a nice day to all.
祝大家度过愉快的一天。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.