ASP.NET MVC: Create Custom Routes
By FoxLearn 9/26/2019 10:59:33 AM 7.95K
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
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.
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: Free Responsive Templates
- ASP.NET MVC: Implement Password Reset with ASP NET Identity
- ASP.NET MVC: Getting Started
- ASP.NET MVC: Create Login Form
- ASP.NET MVC: Login with Google Account
- ASP.NET MVC: Custom properties for IdentityUser
- ASP.NET MVC: Performance Optimization with Bundling and Minification
- ASP.NET MVC: Insert Update Delete and View data from SQL Database
Categories
Popular Posts
How to get Credentials in PowerShell?
10/03/2024
How to sign a powershell script
10/03/2024
How to implement Jint in C#
09/14/2024