根据查询字符串路由到控制器 [英] Route to controller based on query string

查看:86
本文介绍了根据查询字符串路由到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:我们正在从旧系统升级,因此解决方案受到限制.如果存在特定的查询字符串,我将尝试路由到未经授权的控制器.如果不存在,则将用户路由到授权控制器.这是在ASP.Net Core 2.1上.

Problem: We are upgrading from a legacy system, so solutions are constrained. I am trying to route to an unauthorized controller if a specific query string is present. If it is not present, the user is routed to the authorized controller. This is on ASP.Net Core 2.1.

是否可以将控制器设置为基于查询字符串进行路由?我已经尝试过

Is it possible to set the controller to route based on query string? I've tried

[/home/[action]?query = {query}] ->导致运行时错误,原因是?"

[/home/[action]?query={query}] -> Leads to runtime error due to '?'

[/home/[action]/{query}] ->映射到/home/index/1(不是我所需要的)

[/home/[action]/{query}] - > maps to /home/index/1 (not what I need)

感谢您的帮助!

或者,是否可以有一个依赖于查询参数的单独的控制器动作?

Alternatively, is it possible to have a separate controller Action that depends on the query parameter?

public IActionResult Index(){}

public IActionResult Index([FromQuery]string query){}

路由似乎无法区分这两者.

Routing doesn't seem to distinguish between these two.

推荐答案

您可以为此使用 IActionConstraint IParameterModelConvention 接口.简而言之,创建一个这样的IActionConstraint:

You can use IActionConstraint and IParameterModelConvention interfaces for that. In short, create an IActionConstraint like this:

public class RequiredFromQueryActionConstraint : IActionConstraint
{
    private readonly string _parameter;

    public RequiredFromQueryActionConstraint(string parameter)
    {
        _parameter = parameter;
    }

    public int Order => 999;

    public bool Accept(ActionConstraintContext context)
    {
        if (!context.RouteContext.HttpContext.Request.Query.ContainsKey(_parameter))
        {
            return false;
        }

        return true;
    }
}

如果在请求的查询字符串中找不到匹配的参数,则它将从Accept方法返回false.

If a matching parameter is not found on the request's query string, then it will return false from the Accept method.

比这样创建 RequiredFromQueryAttribute 类:

public class RequiredFromQueryAttribute : FromQueryAttribute, IParameterModelConvention
{
    public void Apply(ParameterModel parameter)
    {
        if (parameter.Action.Selectors != null && parameter.Action.Selectors.Any())
        {
            parameter.Action.Selectors.Last().ActionConstraints.Add(new RequiredFromQueryActionConstraint(parameter.BindingInfo?.BinderModelName ?? parameter.ParameterName));
        }
    }
}

比您可以使用以下属性修饰必需的查询字符串参数:

Than you could decorate your mandatory query string parameters with this attribute:

[Route("api/[controller]")]
public class ValuesController : Controller
{
    [HttpGet("{id}")]
    public string Get(int id, [RequiredFromQuery]string foo, [RequiredFromQuery]string bar)
    {
        return id + " " + foo + " " + bar;
    }
}

从现在开始,只有以下URL GET api/values/5?foo = a& bar = b 会导致上述操作,所有其他参数组合都将导致状态响应 404 ,您最终可以将其替换为所需的内容.

From now than, only the following URL GET api/values/5?foo=a&bar=b would lead into the action above, all other combinations of parameters would result in response with status 404, which you can eventually replace with what you want.

您可以在此链接中找到更多信息 https://www.strathweb.com/2016/09/required-query-string-parameters-in-asp-net-core-mvc/

You can find more info at this link https://www.strathweb.com/2016/09/required-query-string-parameters-in-asp-net-core-mvc/

这篇关于根据查询字符串路由到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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