Need help with AspNet.Mvc.TypedRouting?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

ivaylokenov
458 Stars 31 Forks MIT License 52 Commits 15 Opened issues

Description

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

Services available

!
?

Need anything else?

Contributors list

No Data

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(a => a.Index()))

// generating action link Html.ActionLink("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(a => a.Index(With.Any())));
    });
}

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());

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

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

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

// adding route with custom action constraint routes.Add("MyRoute/MyAction", route => route .ToAction(a => a.Index()) .WithActionConstraint(new MyCustomConstraint()));

// adding route to specific HTTP methods routes.Add("MyRoute/MyAction", route => route .ToAction(a => a.Index()) .ForHttpMethods("GET", "POST"));

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

Additionally, you can use typed link generation:

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

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

// generating link with additional route values - /Home/Index/1?key=value urlHelper.Action(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() is just expressive sugar, you can pass 'null' for reference types but it looks ugly urlHelper.Action(c => c.Index(With.No()));

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(c => c.Index(), someObject);

// uses another controller in the expression, additional route values and created object controller.CreatedAtAction(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("RouteName", c => c.Index(), someObject);

// uses route name, another controller in the expression, additional route values and created object controller.CreatedAtRoute("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(c => c.Index());

// uses another controller in the expression and additional route values to return redirect result controller.RedirectToAction(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(c => c.Index());

// uses another controller in the expression and additional route values to return permanent redirect result controller.RedirectToActionPermanent(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("RouteName", c => c.Index());

// uses route name, another controller in the expression and additional route values to return redirect result controller.RedirectToRoute("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("RouteName", c => c.Index());

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

IHtmlHelper extension methods:

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

// generates action link with the link text, the expression and additional route values Html.ActionLink("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("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("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("Route name", "Link text", c => c.Index());

// generates action link with route name, the link text, the expression and additional route values Html.RouteLink("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("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("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(c => c.Index());

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

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

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

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

// begins form to the action from the expression, form method and HTML attributes Html.BeginForm(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("Route name", c => c.Index());

// begins form to the action from the expression and additional route values by specifying route name Html.BeginRouteForm("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("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("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("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("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(c => c.Index());

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

// generates link to the action from the expression with additional route values and protocol urlHelper.Action(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(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(c => c.Index(), new { key = "value" }, "protocol", "hostname", "fragment");

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

// generates link to the action from the expression with additional route values and by specifying route name urlHelper.Link("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.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.