Today I have released the 1.1.0 version of Typed Routing project. I encourage you to try this if you like to have your routes centrally configured, rather than stored in attributes at action level.
You can install it from NuGet using the Strathweb.TypedRouting.AspNetCore package name. Here is whatโs new in this release.
Filter support ๐
You can now directly attach filters to your route definitions - filters that should be executed for a given route. This is equivalent to defining a controller action, and annotating it with a relevant attribute such as action filter or authorization filter.
services.AddMvc(opt =>
{
opt.Get("api/items", c => c.Action<ItemsController>(x => x.Get())).
WithFilters(new AnnotationFilter(), new TimerFilter());
}).EnableTypedRouting();
Filters can also be resolved from ASP.NET Core DI system - as long as they are registered there before.
services.AddSingleton<TimerFilter>();
services.AddMvc(opt =>
{
opt.Get("api/items", c => c.Action<ItemsController>(x => x.Get())).
WithFilter<TimerFilter>();
}).EnableTypedRouting();
Authorization Policy support ๐
The route definitions can also have ASP.NET Core authorization policies attached to them. For example, you can pass in a policy instance:
services.AddMvc(opt =>
{
opt.Get("api/secure", c => c.Action<OtherController>(x => x.Foo()).
WithAuthorizationPolicy(new AuthorizationPolicyBuilder().
RequireAuthenticatedUser().Build()
));
}).EnableTypedRouting();
You can also define a policy as string - then a corresponding policy must be previously registerd in ASP.NET Core DI system.
services.AddAuthorization(o =>
{
o.AddPolicy("MyPolicy", b => b.RequireAuthenticatedUser());
});
services.AddMvc(opt =>
{
opt.Get("api/secure", c => c.Action<OtherController>(x => x.Foo()).
WithAuthorizationPolicy("MyPolicy"));
}).EnableTypedRouting();
New EnableTypedRouting method ๐
In the past typed routing was enabled by calling an extension method on MvcOptions. This is now obsolete, please use the extension method available on IMvcCoreBuilder and IMvcBuilder. The new filters feature, that reaches into the DI requires that.
If you are interested in helping out, need more information or have any comments, please come to the Gihub repo. Thanks!