ivaylokenov / AspNet.Mvc.TypedRouting

A collection of extension methods providing strongly typed routing and link generation for ASP.NET Core MVC projects.
https://mytestedasp.net/
MIT License
485 stars 36 forks source link
asp-net-core asp-net-core-mvc fluent-extensions routing strongly-typed

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" });

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.