ASP.NET Core 中必需的查询字符串参数 [英] Required query string parameter in ASP.NET Core

查看:33
本文介绍了ASP.NET Core 中必需的查询字符串参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 ASP.NET Core 1.1 和 VS2015 (sdk 1.0.0-preview2-003131),我有以下控制器:

Using ASP.NET Core 1.1 with VS2015 (sdk 1.0.0-preview2-003131), I have the following controller:

public class QueryParameters
{
    public int A { get; set; }
    public int B { get; set; }
}

[Route("api/[controller]")]
public class ValuesController : Controller
{
    // GET api/values
    [HttpGet]
    public IEnumerable<string> Get([FromQuery]QueryParameters parameters)
    {
        return new [] { parameters.A.ToString(), parameters.B.ToString() };
    }        
}

如您所见,我有两个查询参数.我想要的是需要其中之一(例如:A).也就是说,我想使用一个属性(如果可能的话)来表示这个属性是必需的.然后,我希望 ASP.NET 在调用我的控制器之前进行此验证.

As you can see, I have two query parameters. What I would like is to have one of them (ex: A) to be required. That is, I would like to use an attribute (if possible) to say that this attribute is required. Then, I would like like ASP.NET to do this validation before even calling my controller.

我希望使用 Newtonsoft RequiredAttribute 来使用与我已经用于验证 PUT/POST 内容中所需属性的相同属性,但由于 url 不是 JSON 字符串,因此显然没有使用.

I would have liked to use the Newtonsoft RequiredAttribute to use the same attributes as I already use to validate the required properties in the PUT/POST content, but since the url is not a JSON string, it is obviously not used.

是否有任何建议让 ASP.NET Core 自动检查所需的查询参数?

Any suggestion to have ASP.NET Core automatically check for required query parameters?

请注意,我知道我可以使用可为空的查询参数自己编写检查代码,但这超出了让 ASP.NET 在调用我的控制器之前进行验证的目的,从而保持我的控制器整洁.

Note that I know that I can code the check myself using nullable query parameters but that beats the purpose of letting ASP.NET do the validation before calling my controller, thus keeping my controller tidy.

推荐答案

可以考虑使用框架的模型绑定特性

You can consider using the model binding feature of the framework

根据此处的文档:使用属性自定义模型绑定行为

MVC 包含几个属性,您可以使用这些属性来指示其默认值模型绑定行为到不同的源.例如,您可以指定属性是否需要绑定,或者是否应该绑定使用 [BindRequired][BindNever] 根本不会发生属性.

MVC contains several attributes that you can use to direct its default model binding behavior to a different source. For example, you can specify whether binding is required for a property, or if it should never happen at all by using the [BindRequired] or [BindNever] attributes.

所以我建议你在模型属性中添加一个BindRequiredAttribute.

So I suggest you add a BindRequiredAttribute to the model property.

public class QueryParameters
{
    [BindRequired]
    public int A { get; set; }
    public int B { get; set; }
}

从那里框架应该能够处理绑定和更新模型状态,以便您可以在操作中检查模型的状态

From there the framework should be able to handle the binding and updating model state so that you can check the state of the model in the action

[Route("api/[controller]")]
public class ValuesController : Controller
{
    // GET api/values
    [HttpGet]
    public IActionResult Get([FromQuery]QueryParameters parameters)
    {    
        if (ModelState.IsValid)
        {
            return Ok(new [] { parameters.A.ToString(), parameters.B.ToString() });
        }
        return BadRequest();
    }        
}

另一种选择是创建一个自定义模型绑定器,如果所需的查询字符串不存在,它会导致操作出错.

The other option would be to create a custom model binder that would fault the action if the required query string is not present.

参考:自定义模型绑定

这篇关于ASP.NET Core 中必需的查询字符串参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆