[英]ASP.NET WebAPI 2: handle empty string query parameters
We have an api used to get data of products: 我们有一个API用于获取产品数据:
public IHttpActionResult GetProducts(ProductFilter productFilter)
{
try
{
.....
}
catch(Exception)
{
throw new OurCustomException();
}
}
and 和
public class ProductFilter
{
[RegularExpression(@"^[a-zA-Z0-9]{11}$")]
public String Number { get; set; }
.....
}
Here is what i want: 这是我想要的:
When send GET /api/products?number=Test1234567
it will return information of a product with the number "Test1234567" 发送GET /api/products?number=Test1234567
,它将返回编号为“ Test1234567”的产品信息
When send GET /api/products?number=
it will return error because the empty string does not match the regex 发送GET /api/products?number=
,它将返回错误,因为空字符串与正则表达式不匹配
When send GET /api/products
it will return information of all products 发送GET /api/products
,它将返回所有产品的信息
So can you suggest me any way to do this by using just Validation Attribute, since we have a common method to handle ValidationException
and we cannot throw ValidationException
from method GetProducts
. 因此,您可以建议我仅使用Validation Attribute来执行此操作的任何方法,因为我们有一个通用的方法来处理ValidationException
并且不能从GetProducts
方法中抛出ValidationException
。 I have tried to use [Required]
and [DisplayFormat(ConvertEmptyStringToNull = false)]
on Number
but none of them worked. 我尝试在Number
上使用[Required]
和[DisplayFormat(ConvertEmptyStringToNull = false)]
,但是它们都不起作用。
And please also inform me if it is impossible. 如果无法实现,也请通知我。
EDIT: I think your problem is that you are not passing full model as parameter - your are getting null
once binder tries to bind query string. 编辑:我认为您的问题是您没有传递完整的模型作为参数-一旦活页夹试图绑定查询字符串,您将为null
。 This is not a problem with your regex. 这不是您的正则表达式的问题。 To get it working you should make q request like GET /api/products?number=&prop1=some_value&prop2=some_value
要使其正常工作,您应该发出q请求,例如GET /api/products?number=&prop1=some_value&prop2=some_value
Original answer: 原始答案:
I think changing your regex to: 我认为将您的正则表达式更改为:
public class ProductFilter
{
[RegularExpression(@"^(|[a-zA-Z0-9]{11})$")]
public String Number { get; set; }
.....
}
should do a trick. 应该做个把戏。
However MSDN documentation states that: 但是,MSDN文档指出:
If the value of the property is null or an empty string (""), the value automatically passes validation for the RegularExpressionAttribute attribute. 如果属性的值为null或空字符串(“”),则该值将自动通过对RegularExpressionAttribute属性的验证。
So it should work anyway. 因此无论如何它应该工作。 Additionally we can check RegularExpression
code: 另外,我们可以检查RegularExpression
代码:
public override bool IsValid(object value)
{
this.SetupRegex();
string input = Convert.ToString(value, (IFormatProvider) CultureInfo.CurrentCulture);
if (string.IsNullOrEmpty(input))
return true;
Match match = this.Regex.Match(input);
if (match.Success && match.Index == 0)
return match.Length == input.Length;
return false;
}
as you can see, it allows null or empty input so your problem is rather different. 如您所见,它允许输入空值或空值,因此您的问题就大不一样了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.