ASP.NET MVC: Create Custom Routes
By FoxLearn 9/26/2019 10:59:33 AM 8.1K
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 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
11 Things You Didn't Know About Cloudflare
12/19/2024