Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A library enabling strongly typed routing in ASP.NET Core MVC projects.

License

NotificationsYou must be signed in to change notification settings

filipw/Strathweb.TypedRouting.AspNetCore

Repository files navigation

A library enabling strongly typed routing in ASP.NET Core MVC projects.

Installation

Everything is onNuget.Nuget

nuget install Strathweb.TypedRouting.AspNetCore

or via the .NET Core CLI:

dotnet add package Strathweb.TypedRouting.AspNetCore

Setup

In yourStartup class, after adding MVC, callAddTypedRouting(); and then configure your routes:

services.AddMvc().AddTypedRouting(opt=>{opt.Get("homepage", c=>c.Action<HomeController>(x=>x.Index()));opt.Get("aboutpage/{name}", c=>c.Action<HomeController>(x=>x.About(Param<string>.Any)));opt.Post("sendcontact", c=>c.Action<HomeController>(x=>x.Contact()));});

This creates:

  • a GET route to/homepage
  • a GET route to/aboutpage/{name}
  • a POST route to/sendcontact

All of which will route to the relevant methods on ourHomeController.

Link generation

Since the API is fluent, you can also give the routes names so that you can use them with i.e. link generation.

opt.Get("api/values/{id}", c=>c.Action<ValuesController>(x=>x.Get(Param<int>.Any))).WithName("GetValueById");

Now you can use it withIUrlHelper (it's aUrl property on every controller):

varlink=Url.Link("GetValueById",new{id=1});

IUrlHelper can also be obtained fromHttpContext, anywhere in the pipeline (i.e. in a filter):

varservices=context.HttpContext.RequestServices;varurlHelper=services.GetRequiredService<IUrlHelperFactory>().GetUrlHelper(context);varlink=urlHelper.Link("GetValueById",new{id=1});

Finally, you can also use this link generation technique with the built-in action results, such as for exampleCreatedAtRouteResult:

publicIActionResultPost([FromBody]stringvalue){varresult=CreatedAtRoute("GetValueById",new{id=1},"foo");returnresult;}

Filters

The route definitions can also be done along with 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().AddTypedRouting(opt=>{opt.Get("api/items", c=>c.Action<ItemsController>(x=>x.Get())).WithFilters(newAnnotationFilter());});

Filters can also be resolved from ASP.NET Core DI system - as long as they are registered there before.

services.AddSingleton<TimerFilter>();services.AddMvc().AddTypedRouting(opt=>{opt.Get("api/items", c=>c.Action<ItemsController>(x=>x.Get())).WithFilter<TimerFilter>();});

Authorization Policies

The route definitions can also have ASP.NET Core authorization policies attached to them.

You can pass in a policy instance:

services.AddMvc().AddTypedRouting(opt=>{opt.Get("api/secure", c=>c.Action<OtherController>(x=>x.Foo()).WithAuthorizationPolicy(newAuthorizationPolicyBuilder().RequireAuthenticatedUser().Build()));});

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().AddTypedRouting(opt=>{opt.Get("api/secure", c=>c.Action<OtherController>(x=>x.Foo()).WithAuthorizationPolicy("MyPolicy"));});

Action constraints

The library supports two ways of specifying MVC action constraints:

  • inline in the template
  • via fluent API

The inline constraints are the same as you can use with attribute routing. For example:

opt.Get("api/other/{id:int}", c=>c.Action<OtherController>(x=>x.Action2(Param<int>.Any)));

You can also specify constraints via the fluent API, by chainingIActionConstraintMetadata implementations. Consider the following sample constraint class:

publicclassMandatoryHeaderConstraint:IActionConstraint,IActionConstraintMetadata{privatestring_header;publicMandatoryHeaderConstraint(stringheader){_header=header;}publicintOrder{get{return0;}}publicboolAccept(ActionConstraintContextcontext){// only allow route to be hit if the predefined header is presentif(context.RouteContext.HttpContext.Request.Headers.ContainsKey(_header)){returntrue;}returnfalse;}}

You can now use this class in the route declaration:

opt.Get("api/other", c=>c.Action<OtherController>(x=>x.Action1())).WithConstraints(newMandatoryHeaderConstraint("CustomHeader"));

License

MIT

About

A library enabling strongly typed routing in ASP.NET Core MVC projects.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors2

  •  
  •  

Languages


[8]ページ先頭

©2009-2025 Movatter.jp