You can achieve this with a custom IActionModelConvention implementation. The official documentation explains the concept of an action model convention: Work with the application model in ASP.NET Core - Conventions. In a nutshell, by implementing IActionModelConvention, you can make changes to the application model and add filters, routes, etc to an action at runtime.
This is best explained with a sample implementation, which follows below. As you want to combine your existing MVC filter with the ability to configure routes for an action, the implementation below implements both IResourceFilter (this can be whatever filter type you're using) and IActionModelConvention:
public class VerifyAccessFilterAttribute : Attribute, IActionModelConvention, IResourceFilter
{
public VerifyAccessFilterAttribute(params string[] routeTemplates)
{
RouteTemplates = routeTemplates;
}
public string[] RouteTemplates { get; set; }
public void Apply(ActionModel actionModel)
{
actionModel.Selectors.Clear();
foreach (var routeTemplate in RouteTemplates)
{
actionModel.Selectors.Add(new SelectorModel
{
AttributeRouteModel = new AttributeRouteModel { Template = routeTemplate },
ActionConstraints = { new HttpMethodActionConstraint(new[] { "GET" }) }
});
}
}
public void OnResourceExecuting(ResourceExecutingContext ctx) { ... }
public void OnResourceExecuted(ResourceExecutedContext ctx) { ... }
}
In this example, it's all about the Apply method, which simply adds a new SelectorModel for each routeTemplate (as I've named it), each of which is constrained to HTTP GET requests.
HttpGettwice btwVerifyAccessFilterbtw? The only hit when googling it is this questionVerifyAccessFilteris a custom filter. The reason for having 2 is that we'd like this method to be responsible for 2 different routes. As you can see the parameters are very similar, so if possible we'd like to merge the 2 attributes.Routeattribute and registers that route. BTW if you use constants for the routes you don't need attributes. You could add them just as easily in the routing configuration