我知道您可以在模型上使用验证属性来验证它:
public class CommunicationQuery
{
[RegularExpression("[0-9]{0,10}", ErrorMessage = "Please enter a valid policy number")]
public string PolicyId { get; set; }
[RegularExpression("[0-9]{0,10}", ErrorMessage = "Please enter a valid member number")]
public string MemberId { get; set; }
}
public IEnumerable<Communication> Get([FromUri]CommunicationQuery documentQuery)
{
}
但是可以使用验证属性验证单个字符串吗?
public async Task<HttpResponseMessage> Get([RegularExpression("[0-9]{0,10}")]string id)
{
}
这似乎不起作用。我能够做到这一点的唯一方法是创建包装器对象并使用[FromUri]
,在操作本身上使用自定义ActionFilterAttribute
或手动验证控制器操作中的参数使用正则表达式。
答案 0 :(得分:5)
其他两个解决方案仅适用于路由属性。但是,如果您想验证查询参数,可以执行以下操作:
public class MinWithStatusAttribute : ParameterBindingAttribute
{
private readonly int _minValue;
public MinWithStatusAttribute(int minValue)
{
_minValue = minValue;
}
public override HttpParameterBinding GetBinding(HttpParameterDescriptor parameter) => new MinWithStatusParameterBinding(parameter, _minValue);
}
public class MinWithStatusParameterBinding : HttpParameterBinding, IValueProviderParameterBinding
{
private readonly int _minValue;
public HttpParameterBinding DefaultUriBinding;
public MinWithStatusParameterBinding(HttpParameterDescriptor desc, int minValue)
: base(desc)
{
_minValue = minValue;
var defaultUrl = new FromUriAttribute();
this.DefaultUriBinding = defaultUrl.GetBinding(desc);
this.ValueProviderFactories = defaultUrl.GetValueProviderFactories(desc.Configuration);
}
public override Task ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancellationToken)
{
return DefaultUriBinding.ExecuteBindingAsync(metadataProvider, actionContext, cancellationToken).ContinueWith((tsk) =>
{
var currentBoundValue = this.GetValue(actionContext);
if (!(currentBoundValue is int)) return; //if it is not an Int, return.
var currentBoundInt = (int)currentBoundValue;
if (currentBoundInt >= _minValue) return; //If the value provided is greater than or equal to the min value, return. Else throw an error
var preconditionFailedResponse = actionContext.Request.CreateResponse(HttpStatusCode.PreconditionFailed, $"The parameter {DefaultUriBinding.Descriptor.ParameterName} must be greater than or equal to {_minValue}" });
throw new HttpResponseException(preconditionFailedResponse);
}, cancellationToken);
}
public IEnumerable<ValueProviderFactory> ValueProviderFactories { get; } //IValueProviderParameterBinding
}
这是一个整数,但您可以轻松修改它以使用字符串正则表达式。现在它可以应用于您的查询参数:
public IHttpActionResult SendEmailToCandidate(int id, [MinWithStatus(3)]int company_id, [MinWithStatus(3)]int recruiter_id, string subject, string body)
{
//Do stuff in your controller method. If they enter less than 3 it will display an error with Http status code 412 precondition failed.
}
答案 1 :(得分:3)
如果您使用属性路由来管理进入控制器的路径, 你可以做这样的事情:
[Route("{Id:regex([0-9]{0,10})}")]
public async Task<HttpResponseMessage> Get(string id)
{
}
Attribute Routing overview documentation中记载了各种路线约束。
它确实提出了一个问题,即为什么您要接受长度为10的数字字符串作为您的ID。在将其解析为不超过2,147,483,647
的int时,您必须要小心,因为它是默认整数的最大大小。