首页 > 解决方案 > Asp.Net Core 2.0+ 中模型验证前的模型规范化

问题描述

我正在使用自动模型验证(请参阅“更好的输入处理”)来保持我的控制器清洁;所以:

[HttpPost]
[ProducesResponseType(typeof(Product), 201)]
public IActionResult Post([FromBody] Product product)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }
    product = _repository.AddProduct(product);
    return CreatedAtAction(nameof(Get), new { id = product.Id }, product);
}

变成:

[HttpPost]
[ProducesResponseType(201)]
public ActionResult<Product> Post(Product product)
{
    _repository.AddProduct(product);
    return CreatedAtAction(nameof(Get), new { id = product.Id }, product);
}

但是,我确实有一些具有电话号码属性的模型。我想在调用模型验证之前对这些进行“规范化”。我的意思是我想string从各种输入中标准化这些属性(类型),例如:

E.164 表示法

因此,无论用户以何种形式输入电话号码,在调用验证之前,我都想确保它始终采用 E.164 形式(“规范化”)。这种标准化是如何完成的是无关紧要的(如果你坚持,我会使用libphonenumber )。作为第二个,也许不那么复杂的例子,我可以想象一个字符串在调用验证之前总是大写/小写。

在调用验证之前调用我的规范化过程的正确或最佳方法是什么?我需要写一些中间件吗?

同样相关:我的模型包含属性,因此规范化器知道要规范化哪些属性(以及如何规范化):

class ExampleModel {

    public int Id { get; set; }

    public string Name { get; set; }

    [NormalizedNumber(NumberFormat.E164)]
    public string Phonenumber { get; set; }
}

我猜中间件(?或任何解决方案)然后可以采用模型,确定是否有任何属性(递归)具有该属性并在需要时调用规范器。

标签: c#asp.net-web-apiasp.net-web-api2asp.net-core-2.1model-validation

解决方案


也许您可以使用 Formatter 使用这样的方法。我在我的 API 中使用了类似的方法将所有传入的日期转换为 UTC 格式

public class JsonModelFormatter : JsonMediaTypeFormatter
{
    public override System.Threading.Tasks.Task<Object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger, CancellationToken cancellationToken)
    {

        System.Threading.Tasks.Task<Object> baseTask = base.ReadFromStreamAsync(type, readStream, content, formatterLogger, cancellationToken);

        if (baseTask.Result != null)
        {
            var properties = baseTask.Result.GetType().GetProperties();
            foreach (var property in properties)
            {
                //Check Property attribute and decide if you need to format it
                if (property.CustomAttributes.Where (x=> you condition here))
                {
                    if (property.CanWrite && property.GetValue(baseTask.Result, null) != null)
                    {
                        var propValue = ((string)property.GetValue(baseTask.Result, null));
                       //Update propValue here 
                       property.SetValue(baseTask.Result, newPropValue);
                    }
                }
            }

        }
        return baseTask;
    }

    public override bool CanReadType(Type type)
    {
        return true;
    }
}

推荐阅读