O. Shai
O. Shai

Reputation: 813

Asp.net Core add several action filters in one filter

I need to create a custom action filter attribute, that contains in it a declaration of 2 "RouteAttibute" filters.

I need:

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

Instead of:

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

Upvotes: 0

Views: 906

Answers (1)

O. Shai
O. Shai

Reputation: 813

Thanks to @Kirk Larkin with his answer here, I've managed to solve this:

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" }
        });
    }

}

Upvotes: 1

Related Questions