Asp .Net MVC - SPLessons

ASP.Net MVC Routing

Home > Lesson > Chapter 10
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

ASP.Net MVC Routing

ASP.Net MVC Routing

What is Routing?

shape Description

Routing is a mechanism to shorten the route. One can create URL to view using routing.

shape Example

To create a URL, one have a long route which is more complex to go to the destination.So, search for a short route to go to destination. This work is done by Routing in MVC. In Technical language, Long route means the URL which is more lengthy. Short route means the URL which one have created using routing and can create any number of URLs for views by using this routing.

How to use Routing?

shape Description

To use the routing, follow the below steps.

shape Step 1

Double click on the RoutingConfig.cs File, which is under the App_Start folder in the Solution Explorer as shown in the below code.

shape Step 2

Then, the following code will appear as shown in the below figure.

shape Step 3

Write the following code to generate URLs. [csharp] using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Routing; namespace HelloWorld { public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Home", // The name of the URL. This must be unique url: "Home", // The URL which we have given defaults: new { controller = "Home", action = "GotoHome", id = UrlParameter.Optional } // The componetes which for which we have created URL ); routes.MapRoute( name: "Default", // Default route url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } } } [/csharp]

shape Step 3

Press f5 to run the application. Then the browser will appear. Now, type the URL as shown in the following window and press Enter. The output will be displayed as shown in the above diagram.