我需要创建一个自定义操作过滤器属性,该属性中包含2个“ RouteAttibute”过滤器的声明。
我需要:
[Contains2Routes]
public ActionResult Index()
{
return View();
}
代替:
[Route("~/index1")]
[Route("~/index2")]
public ActionResult Index()
{
return View();
}
答案 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" }
});
}
}