All Projects → ivaylokenov → Aspnet.mvc.typedrouting

ivaylokenov / Aspnet.mvc.typedrouting

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

Projects that are alternatives of or similar to Aspnet.mvc.typedrouting

AspNetCore-Dynamic-Permission
Dynamic Permission Samples in ASP.NET Core and ASP.NET MVC 5.
Stars: ✭ 19 (-95.87%)
Mutual labels:  asp-net-core, asp-net-core-mvc
Lib.AspNetCore.Mvc.Ndjson
Lib.AspNetCore.Mvc.Ndjson is a library that provides support for NDJSON (Newline Delimited JSON) based structured data streaming to ASP.NET Core MVC
Stars: ✭ 13 (-97.17%)
Mutual labels:  asp-net-core, asp-net-core-mvc
Awesome-Nuget-Packages
📦 A collection of awesome and top .NET packages sorted by most popular needs.
Stars: ✭ 87 (-81.09%)
Mutual labels:  asp-net-core, asp-net-core-mvc
Demo.AspNetCore.WebApi
Sample Web API powered by ASP.NET Core MVC, Azure Cosmos DB and MediatR
Stars: ✭ 24 (-94.78%)
Mutual labels:  asp-net-core, asp-net-core-mvc
ASP.NET-CORE-MVC-Sample-Registration-Login
C# Asp.Net Core MVC Sample Registration/Login/Email Activation Form with MsSQL Server/Entity Framework/Onion Architecture
Stars: ✭ 37 (-91.96%)
Mutual labels:  asp-net-core, asp-net-core-mvc
ProductsStoreOnKubernetes
Demoing deployment of Docker containers into Kubernetes for both minikube and Azure AKS.
Stars: ✭ 90 (-80.43%)
Mutual labels:  asp-net-core, asp-net-core-mvc
Asp-net-Core-Project-with-Admin-Template-Setup
AdminLTE Template Setup with Asp.net Core MVC 2.1 Project
Stars: ✭ 50 (-89.13%)
Mutual labels:  asp-net-core, asp-net-core-mvc
AspNetCore.Unobtrusive.Ajax
Unobtrusive Ajax Helpers (like MVC5 Ajax.BeignForm and Ajax.ActionLink) for ASP.NET Core
Stars: ✭ 46 (-90%)
Mutual labels:  asp-net-core, asp-net-core-mvc
Ext.NET
Ext.NET public Issues.
Stars: ✭ 28 (-93.91%)
Mutual labels:  asp-net-core, asp-net-core-mvc
AuctionSystem
Auction system written in ASP.NET Core
Stars: ✭ 59 (-87.17%)
Mutual labels:  asp-net-core, asp-net-core-mvc
MvcControlsToolkit.Core
Core Code for MvcControlsToolkit packages
Stars: ✭ 13 (-97.17%)
Mutual labels:  asp-net-core, asp-net-core-mvc
Professionalcsharp7
Code samples for the book Professional C# 7 and .NET Core 2.0 (with updates for 2.1), Wrox Press
Stars: ✭ 403 (-12.39%)
Mutual labels:  asp-net-core, asp-net-core-mvc
OnlineUsers-Counter-AspNetCore
Display online users count in ASP.NET Core in two ways (Cookie - SingalR)
Stars: ✭ 29 (-93.7%)
Mutual labels:  asp-net-core, asp-net-core-mvc
ExcelExport
Classes to generate Excel/CSV Report in ASP.NET Core
Stars: ✭ 39 (-91.52%)
Mutual labels:  asp-net-core, asp-net-core-mvc
eShopOnWeb
Sample ASP.NET Core 6.0 reference application, powered by Microsoft, demonstrating a layered application architecture with monolithic deployment model. Download the eBook PDF from docs folder.
Stars: ✭ 8,250 (+1693.48%)
Mutual labels:  asp-net-core, asp-net-core-mvc
DNZ.MvcComponents
A set of useful UI-Components (HtmlHelper) for ASP.NET Core MVC based-on Popular JavaScript Plugins (Experimental project).
Stars: ✭ 25 (-94.57%)
Mutual labels:  asp-net-core, asp-net-core-mvc
AspNetCore.FriendlyExceptions
ASP.NET Core Filter and Middleware to catch exceptions and translate them into nice HTTP responses
Stars: ✭ 17 (-96.3%)
Mutual labels:  asp-net-core, asp-net-core-mvc
PersianDataAnnotations
PersianDataAnnotations is ASP.NET Core MVC & ASP.NET MVC Custom Localization DataAnnotations (Localized MVC Errors) for Persian(Farsi) language - فارسی سازی خطاهای اعتبارسنجی توکار ام.وی.سی. و کور.ام.وی.سی. برای نمایش اعتبار سنجی سمت کلاینت
Stars: ✭ 38 (-91.74%)
Mutual labels:  asp-net-core, asp-net-core-mvc
jQuery-datatable-server-side-net-core
A simple Visual Studio solution using jQuery DataTable with Server-Side processing using .NET 5
Stars: ✭ 71 (-84.57%)
Mutual labels:  asp-net-core, asp-net-core-mvc
AspNetCore.Mvc.FluentActions
Fluent Actions for ASP.NET Core MVC are abstractions of regular MVC actions that are converted into MVC actions during startup.
Stars: ✭ 17 (-96.3%)
Mutual labels:  asp-net-core, asp-net-core-mvc

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].