{"id":13429742,"url":"https://github.com/filipw/Strathweb.TypedRouting.AspNetCore","last_synced_at":"2025-03-16T04:31:01.894Z","repository":{"id":144137105,"uuid":"59043660","full_name":"filipw/Strathweb.TypedRouting.AspNetCore","owner":"filipw","description":"A library enabling strongly typed routing in ASP.NET Core MVC projects.","archived":false,"fork":false,"pushed_at":"2019-01-22T19:42:56.000Z","size":83,"stargazers_count":76,"open_issues_count":9,"forks_count":9,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-09T21:02:09.558Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/filipw.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2016-05-17T17:21:50.000Z","updated_at":"2024-12-25T01:08:25.000Z","dependencies_parsed_at":null,"dependency_job_id":"0f846f54-bdc9-48c5-a39c-a1e7251a8aae","html_url":"https://github.com/filipw/Strathweb.TypedRouting.AspNetCore","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/filipw%2FStrathweb.TypedRouting.AspNetCore","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/filipw%2FStrathweb.TypedRouting.AspNetCore/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/filipw%2FStrathweb.TypedRouting.AspNetCore/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/filipw%2FStrathweb.TypedRouting.AspNetCore/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/filipw","download_url":"https://codeload.github.com/filipw/Strathweb.TypedRouting.AspNetCore/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243826788,"owners_count":20354220,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T02:00:44.519Z","updated_at":"2025-03-16T04:31:01.558Z","avatar_url":"https://github.com/filipw.png","language":"C#","funding_links":[],"categories":["Frameworks, Libraries and Tools","others","框架, 库和工具"],"sub_categories":["Application Frameworks","应用程序框架"],"readme":"# Strathweb.TypedRouting.AspNetCore\n\nA library enabling strongly typed routing in ASP.NET Core MVC projects.\n\n## Installation\n\nEverything is on [Nuget](https://www.nuget.org/packages/Strathweb.TypedRouting.AspNetCore). [![Nuget](http://img.shields.io/nuget/v/Strathweb.TypedRouting.AspNetCore.svg?maxAge=10800)](https://www.nuget.org/packages/Strathweb.TypedRouting.AspNetCore)\n\n```\nnuget install Strathweb.TypedRouting.AspNetCore\n```\nor via the .NET Core CLI:\n\n```\ndotnet add package Strathweb.TypedRouting.AspNetCore\n```\n\n## Setup\n\nIn your `Startup` class, after adding MVC, call `AddTypedRouting();` and then configure your routes:\n\n```csharp\nservices.AddMvc().AddTypedRouting(opt =\u003e\n{\n    opt.Get(\"homepage\", c =\u003e c.Action\u003cHomeController\u003e(x =\u003e x.Index()));\n    opt.Get(\"aboutpage/{name}\", c =\u003e c.Action\u003cHomeController\u003e(x =\u003e x.About(Param\u003cstring\u003e.Any)));\n    opt.Post(\"sendcontact\", c =\u003e c.Action\u003cHomeController\u003e(x =\u003e x.Contact()));\n});\n```\n\nThis creates:\n* a GET route to `/homepage`\n* a GET route to `/aboutpage/{name}`\n* a POST route to `/sendcontact`\n\nAll of which will route to the relevant methods on our `HomeController`.\n\n## Link generation\n\nSince the API is fluent, you can also give the routes names so that you can use them with i.e. link generation.\n\n```csharp\nopt.Get(\"api/values/{id}\", c =\u003e c.Action\u003cValuesController\u003e(x =\u003e x.Get(Param\u003cint\u003e.Any))).WithName(\"GetValueById\");\n```\n\nNow you can use it with `IUrlHelper` (it's a `Url` property on every controller):\n\n```csharp\nvar link = Url.Link(\"GetValueById\", new { id = 1 });\n```\n\n`IUrlHelper` can also be obtained from `HttpContext`, anywhere in the pipeline (i.e. in a filter):\n\n```csharp\nvar services = context.HttpContext.RequestServices;\nvar urlHelper = services.GetRequiredService\u003cIUrlHelperFactory\u003e().GetUrlHelper(context);\nvar link = urlHelper.Link(\"GetValueById\", new { id = 1 });\n```\n\nFinally, you can also use this link generation technique with the built-in action results, such as for example `CreatedAtRouteResult`:\n\n```csharp\npublic IActionResult Post([FromBody]string value)\n{\n    var result = CreatedAtRoute(\"GetValueById\", new { id = 1 }, \"foo\");\n    return result;\n}\n```\n\n## Filters\n\nThe 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.\n\n```csharp\nservices.AddMvc().AddTypedRouting(opt =\u003e\n{\n    opt.Get(\"api/items\", c =\u003e c.Action\u003cItemsController\u003e(x =\u003e x.Get())).WithFilters(new AnnotationFilter());\n});\n```\n\nFilters can also be resolved from ASP.NET Core DI system - as long as they are registered there before.\n\n```csharp\nservices.AddSingleton\u003cTimerFilter\u003e();\n\nservices.AddMvc().AddTypedRouting(opt =\u003e\n{\n    opt.Get(\"api/items\", c =\u003e c.Action\u003cItemsController\u003e(x =\u003e x.Get())).WithFilter\u003cTimerFilter\u003e();\n});\n```\n\n## Authorization Policies\n\nThe route definitions can also have ASP.NET Core authorization policies attached to them.\n\nYou can pass in a policy instance:\n\n```csharp\nservices.AddMvc().AddTypedRouting(opt =\u003e\n{\n        opt.Get(\"api/secure\", c =\u003e c.Action\u003cOtherController\u003e(x =\u003e x.Foo()).\n                WithAuthorizationPolicy(new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build()));\n});\n```\n\nYou can also define a policy as string - then a corresponding policy must be previously registerd in ASP.NET Core DI system.\n\n```csharp\nservices.AddAuthorization(o =\u003e\n{\n        o.AddPolicy(\"MyPolicy\", b =\u003e b.RequireAuthenticatedUser());\n});\n\nservices.AddMvc().AddTypedRouting(opt =\u003e\n{\n        opt.Get(\"api/secure\", c =\u003e c.Action\u003cOtherController\u003e(x =\u003e x.Foo()).\n                WithAuthorizationPolicy(\"MyPolicy\"));\n});\n```\n\n## Action constraints\n\nThe library supports two ways of specifying MVC action constraints:\n\n - inline in the template\n - via fluent API\n\nThe inline constraints are the same as you can use with attribute routing. For example:\n\n```csharp\nopt.Get(\"api/other/{id:int}\", c =\u003e c.Action\u003cOtherController\u003e(x =\u003e x.Action2(Param\u003cint\u003e.Any)));\n```\n\nYou can also specify constraints via the fluent API, by chaining `IActionConstraintMetadata` implementations. Consider the following sample constraint class:\n\n```csharp\n    public class MandatoryHeaderConstraint : IActionConstraint, IActionConstraintMetadata\n    {\n        private string _header;\n\n        public MandatoryHeaderConstraint(string header)\n        {\n            _header = header;\n        }\n\n        public int Order\n        {\n            get\n            {\n                return 0;\n            }\n        }\n\n        public bool Accept(ActionConstraintContext context)\n        {\n            // only allow route to be hit if the predefined header is present\n            if (context.RouteContext.HttpContext.Request.Headers.ContainsKey(_header))\n            {\n                return true;\n            }\n\n            return false;\n        }\n    }\n```\n\nYou can now use this class in the route declaration:\n\n```csharp\nopt.Get(\"api/other\", c =\u003e c.Action\u003cOtherController\u003e(x =\u003e x.Action1())).WithConstraints(new MandatoryHeaderConstraint(\"CustomHeader\"));\n```\n\n## License\n\n[MIT](https://github.com/filipw/Strathweb.TypedRouting.AspNetCore/blob/master/LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffilipw%2FStrathweb.TypedRouting.AspNetCore","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffilipw%2FStrathweb.TypedRouting.AspNetCore","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffilipw%2FStrathweb.TypedRouting.AspNetCore/lists"}