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

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

问题描述

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

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天全站免登陆