URL Routing in ASP.NET MVC, url routing in asp, routing in mvc, url routing in asp.net, routing in asp.net
Hello friends, I hope you all are doing great. In today's tutorial, we will have a look at URL Routing in ASP.NET MVC. It's the 8th tutorial in this ASP.NET MVC series. Today's tutorial is on a new concept in asp.net and it's essential to understand URL routing before going any further. URL Routing is actually a part of core ASP.NET but MVC applications also use it. URL Routing is implemented by System.Web.Routing and ASP.NET MVC uses System.Web.Routing. So, let's have a look at what's URL Routing in ASP.NET MVC:

URL Routing in ASP.NET MVC

  • URL Routing is used for directing the HTTP request (generated by the user) to the respective controller in ASP.NET MVC.
  • Whenever a user types some url in the browser and hit enter then an HTTP request is generated and this HTTP request is then handled by the controller.
  • This assigning of HTTP request to controller is done by URL Routing.
  • In ASP.NET MVC application, a default Routing file is automatically created in the project.
  • In your Solution Explorer, Click on the folder App_Start.
  • In this folder, you will find a file named RouteConfig.cs that's our URL Routing file, all our settings are saved in it.
  • So, open this file in your workspace, as shown in below figure:
URL Routing in ASP.NET MVC, url routing in asp, routing in mvc, url routing in asp.net, routing in asp.net
  • Now you can see this RouteConfig.cs file in above figure, it has some default code in it.
  • Let's have a close look at this code and understand it:
URL Routing in ASP.NET MVC, url routing in asp, routing in mvc, url routing in asp.net, routing in asp.net
  • In this file, we have a MapRoute extension method of RouteCollection, which is a property of RouteTable class.
  • In this MapRoute method, we have three lines of codes, This code is actually setting the URL for Home Page i.e. the index file will open up when you open Home Page.
  • First line is name of our Route and in this case its Default.
  • The second line specifies the URL parameters and you can see it specifies the url syntax, first we have the Controller, after that action from that Controller, and the third parameter is id.
  • So, actually we want to assign the URL request to some Controller and then assign a action from that controller.
  • In the 3rd line, we have assigned default parameters to our variables in URL.
  • So, the Controller we have here is Home and the action we have assigned is Index and id is optional for now.
  • Now if you recall from our previous tutorial Controllers in ASP.NET MVC that in our HomeController file we have a method called Index, as shown in below figure:
URL Routing in ASP.NET MVC, url routing in asp, routing in mvc, url routing in asp.net, routing in asp.net
  • So, I hope you have understood How this URL Routing is working.
  • Let me summarize it, by default we have a URL Routing File and it has the MapRoute extension method for the Home Page and from HomeController file index action is assigned to the URL.
  • This index action is further assigned to index View and thus it open the index page.
  • Now, let's say you want to change your Home Page from index to About.
  • So, you need to do these changes in the Route file:
URL Routing in ASP.NET MVC, url routing in asp, routing in mvc, url routing in asp.net, routing in asp.net
  • As you can see in the above code that I have just changed the action from index to About.
  • If you remember, our HomeController also has another method About, which is assigned to About View.
  • So, now my URL Route will open the Home Controller and then will run the About method and in turn About View will open up for the user.
  • Now when you open the Home Page of your site then About Page will be open up.
  • Here's the video demonstration of URL Routing ASP.NET MVC:
So, that was all about URL Routing in ASP.NET MVC. I hope you have enjoyed today's tutorial. If you have any questions, then ask in comments. Will meet you guys in the next tutorial, take care !!!