ASP.NET MVC: Create Custom Routes
By FoxLearn 9/26/2019 10:59:33 AM 8.38K
This article shows you How to Create a Custom Route in ASP.NET Identity MVC 5 using Entity Framework Code First in C# .NET
ASP.NET routing enables you to use URLs that do not have to map to specific files in a web site. Routing is the process of directing an HTTP request to a controller and the functionality of this processing is implemented in System.Web.Routing
Creating aTestController, then add the Action1, Action2, Action3, Action4 to the TestController
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
public class TestController : Controller { // GET: Test public ActionResult Index() { return View(); } public ActionResult Action1( int id) //default parameter id, you don't need to add to RouteConfig class { return View(); } public ActionResult Action2( int id, string role) //You need to add a route to RouteConfig class { return View(); } public ActionResult Action3( int index) //Add to RouteConfig class { return View(); } public ActionResult Action4( int page, string role) //Add to RouteConfig class { return View(); } } |
Open the RouteConfig class, then add your routes as the following c# code.
1 2 3 4 5 6 7 8 9 10 11 12 |
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute( "{resource}.axd/{*pathInfo}" ); routes.MapRoute( "Test/Action3" , "Test/Action3/{index}" , new { controller = "Test" , action = "Action3" , index = UrlParameter.Optional }); routes.MapRoute( "Test/Action2" , "Test/Action2/{role}/{id}" , new { controller = "Test" , action = "Action2" , role = UrlParameter.Optional, id = UrlParameter.Optional }); routes.MapRoute( "Test/Action4" , "Test/Action4/{role}/{page}" , new { controller = "Test" , action = "Action4" , page = UrlParameter.Optional, role = UrlParameter.Optional }); routes.MapRoute( name: "Default" , url: "{controller}/{action}/{id}" , defaults: new { controller = "Home" , action = "Index" , id = UrlParameter.Optional } ); } |
For example: https://foxlearn.com/Product/Details/1
The default route maps this URL to the following parameters:
+ controller = Product
+ action = Details
+ id = 1
VIDEO TUTORIAL
- ASP.NET MVC Responsive Templates Free Download
- How to upload file in ASP.NET MVC
- How to Create Contact Form Flat Responsive in ASP.NET MVC
- How to check if HttpPostedFileBase is an image
- How to upload Multiple File in ASP.NET MVC
- ASP.NET MVC: Implement Password Reset with ASP NET Identity
- ASP.NET MVC: Getting Started
- ASP.NET MVC: Create Login Form
Categories
Popular Posts
tsParticles Authentication Template
11/17/2024
AdminKit Bootstrap 5 HTML5 UI Kits Template
11/17/2024
Materio Admin Template
11/14/2024
RuangAdmin Template
11/17/2024