简体   繁体   English

ASP.NET WebAPI 2:处理空字符串查询参数

[英]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: 这是我想要的:

  1. When send GET /api/products?number=Test1234567 it will return information of a product with the number "Test1234567" 发送GET /api/products?number=Test1234567 ,它将返回编号为“ Test1234567”的产品信息

  2. When send GET /api/products?number= it will return error because the empty string does not match the regex 发送GET /api/products?number= ,它将返回错误,因为空字符串与正则表达式不匹配

  3. 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.

相关问题 防止 ASP.NET WebAPI 2 将空字符串查询参数视为 null - Prevent ASP.NET WebAPI 2 from treating empty string query parameter as null ASP.NET WebAPI 2:如何将空字符串作为 URI 中的参数传递 - ASP.NET WebAPI 2: How to pass empty string as parameter in URI 带路由参数的asp.net WebApi 2路由查询字符串json - asp.net WebApi 2 routing query string json with route parameter 在Asp.Net WebApi操作中使用FromUri属性时,如果没有路由或查询字符串参数,是否可以强制实例化复杂的类? - Can I force a complex class to be instantiated when using FromUri attribute in Asp.Net WebApi action without route or query string parameters? ASP.NET WebApi路由和参数类型 - ASP.NET WebApi Routes and parameters types ASP.NET WebAPI路由参数 - ASP.NET webapi route parameters 如何在ASP.NET SqlDataSource中处理空的CONTAINS()查询参数? - How to handle empty CONTAINS() query parameter in ASP.NET SqlDataSource? 在ASP.NET WebAPI中添加处理SQL连接字符串的方法的适当位置在哪里 - Where is the proper place to add a method to handle SQL Connection String in ASP.NET WebAPI asp.net 核心 3 webapi:名称中带有连字符的路由或绑定参数(查询或发布) - asp.net core 3 webapi : Route or Bind parameters (query or posted) with hyphens in their name JavaScript 在调用 ASP.Net WebAPI 端点时获取返回空字符串的调用 - JavaScript fetch call returning empty string when calling ASP.Net WebAPI endpoint
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM