What is Routing
URL Routing or URL Rewriting is a technique using that we can define user friendly URL which is useful for Search Engine Optimization (SEO).
Using ASP.NET routing or ASP.NET MVC Routing we can achieve that easily.
Rather than calling Physical File name like 'Home/about.aspx' we can use 'Home/about', User cannot remember files extension
like .aspx or .php so we can use Routing and make url easy to remember for them.
Routing in ASP.NET
Lets create ASP.NET Routing first.
Step 1: Create a new Web Application Project and add 2 pages like Default.aspx and About.aspx.
Step 2: Add Global Application class 'Global.asax'.
Step 3: Add reference System.Web.Routing in your project.
Step 4: Open
global.asax file and add the following code in '
Application_Start'
- void Application_Start(object sender, EventArgs e)
- {
-
-
- System.Web.Routing.RouteTable.Routes.MapPageRoute("Home","Home/Index","~/Default.aspx");
-
- System.Web.Routing.RouteTable.Routes.MapPageRoute("About","Home/About-WDI","~/About.aspx");
-
- }
Here we are going to use RouteTable for Routing url, RouteTable.Routes.MapPageRoute method ask for 3 parameter, that is 'routeName', 'routeUrl' and 'physicalFile' rather than calling Default.aspx, user can call Home/Index.
Now run the application and enter url like '
localhost:/Home/Index' it will automatically invoke Default.aspx page.
Same way call Home/About-WDI as in the following screenshot,
Routing in ASP.NET MVC
Routing in MVC is easy, here we have Route.Config.cs file in App_Start Folder, You can define Routes in that file,
By default route is: Home controller - Index Method.
-
- routes.MapRoute(
- name: "Default",
- url: "{controller}/{action}/{id}",
- defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
routes.MapRoute has attributes like name, url and defaults like controller name, action and id (optional).
Now let us create one MVC Application and open RouteConfig.cs file and add the following custom route.
-
- routes.MapRoute(
- name: "about",
- url: "Home/About-WDI",
- defaults: new { controller = "Home", action = "About", id = UrlParameter.Optional }
- );
It will invoke 'Home' controller '
About' Action Method.
So for example if user hits url : '
localhost:/Home/About-WDI' it will invoke Home controller '
About' Action Method.
Parameter Id is optional here, it can be used while working with id Field like Home/editProduct/3
Enable Attributes Routing
You can also Enable Attributes routing by writing the following code in Route.config.cs file.
-
- routes.MapMvcAttributeRoutes();
Now rather than writing and managing Routes from Route.config you can manage routes from Controller. As in the following screenshot we have added [Route("Home/ContactUs")] above Contact() ActionResult.
- [Route("Home/ContactUs")]
- public ActionResult Contact()
- {
- ViewBag.Message = "Your contact page.";
-
- return View();
- }
Now run your application and enter url like: 'localhost:/Home/contactUs' invokes Contact Method.
Hope you like this article. Same way if you are working with areas then you can manage the routes from AreaRegistration.cs file.
Please download code files for more details.