Asp.net Core在一个过滤器中添加了几个动作过滤器

时间:2018-11-27 08:03:11

标签: c# asp.net-core asp.net-core-mvc

我需要创建一个自定义操作过滤器属性,该属性中包含2个“ RouteAttibute”过滤器的声明。

我需要:

[Contains2Routes]
public ActionResult Index()
{
    return View();
}

代替:

[Route("~/index1")]
[Route("~/index2")]
public ActionResult Index()
{
    return View();
}

1 个答案:

答案 0 :(得分:1)

感谢@Kirk Larkin和他的回答here,我设法解决了这个问题:

public class Contains2RoutesAttribute : Attribute, IActionModelConvention
{

    public void Apply(ActionModel action)
    {
        action.Selectors.Clear();

        // Adding route 1:
        action.Selectors.Add(new SelectorModel
        {
            AttributeRouteModel = new AttributeRouteModel { Template = "~/index1" }
        });

        // Adding route 2:
        action.Selectors.Add(new SelectorModel
        {
            AttributeRouteModel = new AttributeRouteModel { Template = "~/index2" }
        });
    }

}