Understand basic of Asp.net MVC routing

I think in MVC, it is important to understand URL structure first, identifying and knowing how to access view page. I mean in Asp.net website application, we know that which page is being accessed but which is not same in MVC. See the different below.

Asp.net MVC : http://localhost:56084

If you create new MVC project and debug, you notice in the URL there is no page name as compare to Asp.net website. Of course, routing has been applied here. Now you go to App_Start > RouteConfig.cs file, you see this code :

                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }

By default, above route has been added which actually point to HomeController and Index action method. We will not discuss routing in depth here but this will make you understand that how to access your view page.

Now see url attribute in above MapRoute code. This is the actual syntax we use in MVC to access any view page. url: “{controller}/{action}/{id}”


Type this in your browser : http://localhost:56084/Home/Index or http://localhost:56084/Home
You will be accessing same home page.

Note : In MVC, Index is default action method for controller similar to default.aspx and index.aspx page in asp.net.

Similarly, if you want to access contact page, type http://localhost:56084/Home/Contact

I hope that’s enough to know what is routing concept in MVC. We will walkthrough of Asp.net MVC request life cycle in next part of this series.

Do let me know if you have any question.

Leave a Reply

Your email address will not be published. Required fields are marked *