• Stars
    star
    486
  • Rank 87,410 (Top 2 %)
  • Language
    C#
  • License
    MIT License
  • Created over 8 years ago
  • Updated almost 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A collection of extension methods providing strongly typed routing and link generation for ASP.NET Core MVC projects.

AspNet.Mvc.TypedRoutingΒ  AspNet.Mvc.TypedRouting - Typed routing
Β  and link generation for ASP.NET Core MVC

====================================

Resolving controller and action names for various purposes in ASP.NET MVC was always unreliable because the framework uses magic strings in its methods (for example Url.Action("Action", "Controller")). With the C# 6.0 nameof operator, the problem was partially solved. However, nameof cannot be used with various MVC Core features like ActionNameAttribute, AreaAttribute, RouteValueAttribute, IControllerModelConvention, IActionModelConvention, IParameterModelConvention and more. Here comes AspNet.Mvc.TypedRouting to the rescue!

This package gives you typed expression based routing and link generation in a ASP.NET Core MVC web application. Currently working with version 1.1.0.

For example:

// adding route to specific action
routes.Add("MyRoute/{id}", route => route.ToAction<HomeController>(a => a.Index()))

// generating action link
Html.ActionLink<HomeController>("Index", c => c.Index())

Build status license NuGet Badge

Installation

You can install this library using NuGet into your web project. There is no need to add any namespace usings since the package uses the default ones to add extension methods.

Install-Package AspNet.Mvc.TypedRouting

For other interesting packages check out:

How to use

Just add AddTypedRouting() after AddMvc into your Startup class:

public void ConfigureServices(IServiceCollection services)
{
	services.AddMvc().AddTypedRouting();
}

You can check the provided sample to see a working web application with this library.

To register a typed route into your application, add the following line:

public void ConfigureServices(IServiceCollection services)
{
	services.AddMvc().AddTypedRouting(routes =>
	{
		routes.Get("MyRoute/{id}", route => route.ToAction<HomeController>(a => a.Index(With.Any<int>())));
	});
}

This will register route http://mysite.com/MyRoute/{id} to match 'HomeController', 'Index' action with any integer as 'id'. Full list of available methods:

// adding route to specific controller and action name taken from name of method
routes.Add("MyRoute/{action}", route => route.ToController<HomeController>());

// adding route to specific action without parameters
routes.Add("MyRoute/MyAction", route => route.ToAction<HomeController>(a => a.Index()));

// adding route to specific action with any parameters 
// * With.Any<TParameter>() is just expressive sugar, you can pass any value
routes.Add("MyRoute/MyAction/{id}", route => route.ToAction<HomeController>(a => a.Index(With.Any<int>())));

// adding route with specific name
routes.Add("MyRoute/MyAction", route => route
	.ToAction<HomeController>(a => a.Index())
	.WithName("RouteName"));

// adding route with custom action constraint
routes.Add("MyRoute/MyAction", route => route
	.ToAction<HomeController>(a => a.Index())
	.WithActionConstraint(new MyCustomConstraint()));
	
// adding route to specific HTTP methods
routes.Add("MyRoute/MyAction", route => route
	.ToAction<HomeController>(a => a.Index())
	.ForHttpMethods("GET", "POST"));

// you can also specify methods without magic strings
routes.Get("MyRoute/MyAction", route => route.ToAction<HomeController>(a => a.Index()));
routes.Post("MyRoute/MyAction", route => route.ToAction<HomeController>(a => a.Index()));
routes.Put("MyRoute/MyAction", route => route.ToAction<HomeController>(a => a.Index()));
routes.Delete("MyRoute/MyAction", route => route.ToAction<HomeController>(a => a.Index()));

Additionally, you can use typed link generation:

// generating link without parameters - /Home/Index
urlHelper.Action<HomeController>(c => c.Index());

// generating link with parameters - /Home/Index/1
urlHelper.Action<HomeController>(c => c.Index(1));

// generating link with additional route values - /Home/Index/1?key=value
urlHelper.Action<HomeController>(c => c.Index(1), new { key = "value" });

// generating link where action needs parameters to be compiled, but you do not want to pass them - /Home/Index
// * With.No<TParameter>() is just expressive sugar, you can pass 'null' for reference types but it looks ugly
urlHelper.Action<HomeController>(c => c.Index(With.No<int>()));

All methods resolve all kinds of route changing features like ActionNameAttribute, AreaAttribute, RouteConstraintAttribute, IControllerModelConvention, IActionModelConvention, IParameterModelConvention and potentially others. The expressions use the internally created by the MVC framework ControllerActionDescriptor objects, which contain all route specific information.

Controller extension methods:

// uses the same controller in the expression and created object
controller.CreatedAtAction(c => c.Index(), someObject);

// uses the same controller in the expression, additional route values and created object
controller.CreatedAtAction(c => c.Index(), new { key = "value" }, someObject);

// uses another controller in the expression and created object
controller.CreatedAtAction<HomeController>(c => c.Index(), someObject);

// uses another controller in the expression, additional route values and created object
controller.CreatedAtAction<HomeController>(c => c.Index(), new { key = "value" }, someObject);

// uses route name, the same controller in the expression and created object
controller.CreatedAtRoute("RouteName", c => c.Index(), someObject);

// uses route name, the same controller in the expression, additional route values and created object
controller.CreatedAtRoute("RouteName", c => c.Index(), new { key = "value" }, someObject);

// uses route name, another controller in the expression and created object
controller.CreatedAtRoute<HomeController>("RouteName", c => c.Index(), someObject);

// uses route name, another controller in the expression, additional route values and created object
controller.CreatedAtRoute<HomeController>("RouteName", c => c.Index(), new { key = "value" }, someObject);

// uses the same controller in the expression to return redirect result
controller.RedirectToAction(c => c.Index());

// uses the same controller in the expression and additional route values to return redirect result
controller.RedirectToAction(c => c.Index(), new { key = "value" });

// uses another controller in the expression to return redirect result
controller.RedirectToAction<HomeController>(c => c.Index());

// uses another controller in the expression and additional route values to return redirect result
controller.RedirectToAction<HomeController>(c => c.Index(), new { key = "value" });

// uses the same controller in the expression to return permanent redirect result
controller.RedirectToActionPermanent(c => c.Index());

// uses the same controller in the expression and additional route values to return permanent redirect result
controller.RedirectToActionPermanent(c => c.Index(), new { key = "value" });

// uses another controller in the expression to return permanent redirect result
controller.RedirectToActionPermanent<HomeController>(c => c.Index());

// uses another controller in the expression and additional route values to return permanent redirect result
controller.RedirectToActionPermanent<HomeController>(c => c.Index(), new { key = "value" });

// uses route name, the same controller in the expression to return redirect result
controller.RedirectToRoute("RouteName", c => c.Index());

// uses route name, the same controller in the expression and additional route values to return redirect result
controller.RedirectToRoute("RouteName", c => c.Index(), new { key = "value" });

// uses route name, another controller in the expression to return redirect result
controller.RedirectToRoute<HomeController>("RouteName", c => c.Index());

// uses route name, another controller in the expression and additional route values to return redirect result
controller.RedirectToRoute<HomeController>("RouteName", c => c.Index(), new { key = "value" });

// uses route name, the same controller in the expression to return permanent redirect result
controller.RedirectToRoutePermanent("RouteName", c => c.Index());

// uses route name, the same controller in the expression and additional route values to return permanent redirect result
controller.RedirectToRoutePermanent("RouteName", c => c.Index(), new { key = "value" });

// uses route name, another controller in the expression to return permanent redirect result
controller.RedirectToRoutePermanent<HomeController>("RouteName", c => c.Index());

// uses route name, another controller in the expression and additional route values to return permanent redirect result
controller.RedirectToRoutePermanent<HomeController>("RouteName", c => c.Index(), new { key = "value" });

IHtmlHelper extension methods:

// generates action link with the link text and the expression
Html.ActionLink<HomeController>("Link text", c => c.Index());

// generates action link with the link text, the expression and additional route values
Html.ActionLink<HomeController>("Link text", c => c.Index(), new { key = "value" });

// generates action link with the link text, the expression, additional route values and HTML attributes
Html.ActionLink<HomeController>("Link text", c => c.Index(), new { key = "value" }, new { @class = "my-class" });

// generates action link with the link text, the expression, protocol, host name, fragment, additional route values and HTML attributes
Html.ActionLink<HomeController>("Link text", c => c.Index(), "protocol", "hostname", "fragment", new { key = "value" }, new { @class = "my-class" });

// generates action link with route name, the link text and the expression
Html.RouteLink<HomeController>("Route name", "Link text", c => c.Index());

// generates action link with route name, the link text, the expression and additional route values
Html.RouteLink<HomeController>("Route name", "Link text", c => c.Index(), new { key = "value" });

// generates action link with route name, the link text, the expression, additional route values and HTML attributes
Html.RouteLink<HomeController>("Route name", "Link text", c => c.Index(), new { key = "value" }, new { @class = "my-class" });

// generates action link with route name, the link text, the expression, protocol, host name, fragment, additional route values and HTML attributes
Html.RouteLink<HomeController>("Route name", "Link text", c => c.Index(), "protocol", "hostname", "fragment", new { key = "value" }, new { @class = "my-class" });

// begins form to the action from the expression
Html.BeginForm<HomeController>(c => c.Index());

// begins form to the action from the expression and additional route values
Html.BeginForm<HomeController>(c => c.Index(), new { key = "value" });

// begins form to the action from the expression and form method
Html.BeginForm<HomeController>(c => c.Index(), FormMethod.Post);

// begins form to the action from the expression, additional route values and form method
Html.BeginForm<HomeController>(c => c.Index(), new { key = "value" }, FormMethod.Post);

// begins form to the action from the expression, form method and HTML attributes
Html.BeginForm<HomeController>(c => c.Index(), FormMethod.Post, new { @class = "my-class" });

// begins form to the action from the expression, form method and HTML attributes
Html.BeginForm<HomeController>(c => c.Index(), new { key = "value" }, FormMethod.Post, new { @class = "my-class" });

// begins form to the action from the expression by specifying route name
Html.BeginRouteForm<HomeController>("Route name", c => c.Index());

// begins form to the action from the expression and additional route values by specifying route name
Html.BeginRouteForm<HomeController>("Route name", c => c.Index(), new { key = "value" });

// begins form to the action from the expression and form method by specifying route name
Html.BeginRouteForm<HomeController>("Route name", c => c.Index(), FormMethod.Post);

// begins form to the action from the expression, additional route values and form method by specifying route name
Html.BeginRouteForm<HomeController>("Route name", c => c.Index(), new { key = "value" },  FormMethod.Post);

// begins form to the action from the expression, form method and HTML attributes by specifying route name
Html.BeginRouteForm<HomeController>("Route name", c => c.Index(), FormMethod.Post, new { @class = "my-class" });

// begins form to the action from the expression, form method and HTML attributes by specifying route name
Html.BeginRouteForm<HomeController>("Route name", c => c.Index(), new { key = "value" }, FormMethod.Post, new { @class = "my-class" });
  • Note: All form generation methods have additional overloads which allow adding an anti-forgery token.

IUrlHelper extension methods:

// generates link to the action from the expression
urlHelper.Action<HomeController>(c => c.Index());

// generates link to the action from the expression with additional route values
urlHelper.Action<HomeController>(c => c.Index(), new { key = "value" });

// generates link to the action from the expression with additional route values and protocol
urlHelper.Action<HomeController>(c => c.Index(), new { key = "value" }, "protocol");

// generates link to the action from the expression with additional route values, protocol and host name
urlHelper.Action<HomeController>(c => c.Index(), new { key = "value" }, "protocol", "hostname");

// generates link to the action from the expression with additional route values, protocol, host name and fragment
urlHelper.Action<HomeController>(c => c.Index(), new { key = "value" }, "protocol", "hostname", "fragment");

// generates link to the action from the expression by specifying route name
urlHelper.Link<HomeController>("Route name", c => c.Index());

// generates link to the action from the expression with additional route values and by specifying route name
urlHelper.Link<HomeController>("Route name", c => c.Index(), new { key = "value" });

Overloads for asynchronous actions are also available. All methods are well documented, tested and resolve route values successfully.

Licence

Code by Ivaylo Kenov. Copyright 2015-2016 Ivaylo Kenov.

This package has MIT license. Refer to the LICENSE for detailed information.

Any questions, comments or additions?

If you have a feature request or bug report, leave an issue on the issues page or send a pull request. For general questions and comments, use the StackOverflow forum.

More Repositories

1

MyTested.AspNetCore.Mvc

Fluent testing library for ASP.NET Core MVC.
C#
1,712
star
2

MyTested.WebApi

Fluent testing framework for ASP.NET Web API 2.
C#
758
star
3

Code.It.Up.TV

YouTube Channel for Advanced C# Lessons
JavaScript
514
star
4

Architecture-of-ASP.NET-Core-Microservices-Applications

Architecture of ASP.NET Core Microservices Applications
C#
281
star
5

ASP.NET-Server-Architectures

C#
185
star
6

CSharp-Web-Server

C#
185
star
7

Software-Architecture-Series

Become a better software engineer πŸ‘ˆ
162
star
8

C-Sharp-Async-Await-In-Detail

JavaScript
156
star
9

ASP.NET-Core-Project-Car-Renting-System

C#
142
star
10

ASP.NET-MVC-Lambda-Expression-Helpers

A collection of extension methods providing strongly typed link generation for ASP.NET MVC 5 projects.
C#
139
star
11

Domain-Driven-Design-with-ASP.NET-Core-Microservices

Domain-Driven Design with ASP.NET Core Microservices
C#
123
star
12

AspNetCore.Mvc.HttpActionResults

A collection of HTTP status code action results and controller extension methods for ASP.NET Core MVC projects.
C#
100
star
13

CSharp-ORM-Battle

C#
100
star
14

Identity-Server-Demystified

C#
93
star
15

Docker-From-ABC-To-XYZ

93
star
16

Let-s-Get-Functional-With-C-Sharp

C#
86
star
17

MyCoolWebServer

C#
84
star
18

Microservice-Based-Applications

80
star
19

Microservices-Eventual-Consistency-Done-Right

C#
79
star
20

Process-Automation-with-ASP.NET-Core-Microservices

C#
75
star
21

C-Sharp-API-Scenarios-REST-GraphQL-gRPC

C#
64
star
22

GameStoreSimpleMvc

C#
55
star
23

JustChess

Console Chess Game for the OOP course at Telerik Academy
C#
54
star
24

MyTested.HttpServer

Fluent testing framework for remote HTTP servers.
C#
51
star
25

Ticketing-System-ASP.NET-MVC-5

Exam preparation for the Telerik Academy ASP.NET MVC 5 course.
C#
46
star
26

Telerik-Academy-Courses

MEAN Stack Application
CSS
45
star
27

Music-Artists

JavaScript
38
star
28

TestRepoCode

Testing GitHub
C#
36
star
29

express-architecture

JavaScript
36
star
30

express-template

Per type architecture for Node.js, Express, Mongoose and Handlebars.
JavaScript
35
star
31

AzurePipelinesBlog

C#
34
star
32

LearningSystemDemo

C#
32
star
33

ModPanel

C#
31
star
34

Jetpack-Joyride-Unity-3D

C#
30
star
35

Cool-Demo-Project

This is my cool demo project for a SoftUni lecture!
C#
27
star
36

AngularJS-SocialNetwork

JavaScript
25
star
37

ExpressionTrees

C#
25
star
38

ivaylokenov

24
star
39

Telerik-Academy-Tasks

My programming experience at Telerik Academy
JavaScript
23
star
40

Flappy-Bird-Unity-3D

C#
20
star
41

TestGitRepo

This is a test repository.
20
star
42

mocking-demo

C#
19
star
43

EasyPTC

C#
19
star
44

Student-System-Live-Demo

Entity Framework Code First and Repository Pattern Live Demo
JavaScript
18
star
45

DynamicXMLBuilder

C#
18
star
46

MVC.Models

JavaScript
17
star
47

TelerikHackaton.Ruler

C#
16
star
48

Roslyn-Overview

Overview of the .NET Compiler Platform
C#
16
star
49

Street-Fitness-Kendo-Mobile

KendoMobileStreetFitness
JavaScript
13
star
50

SoftUniDemoGitHubProject

Demo project for SoftUni Fundamentals
12
star
51

TestGit

Using this repository for a GitHub lecture at SoftUni
JavaScript
10
star
52

FundamentalsGitHubDemo

This is a demo repository for a lecture at SoftUni.
JavaScript
4
star