1. Back To Blog

5 important ASP.NET MVC routing interview questions

ASP.NET MVC routing is one of favorite topics during MVC interviews. In this article we will try to go through some important questions around MVC routing section. If you are new to MVC and want to start learning you can start from this MVC youtube video



What are routing in MVC?

Routing helps you to define user friendly URL structure and map those URL structure to the controller.

For instance let's say we want that when any user types "http://localhost/View/ViewCustomer/" , it goes to the "Customer" Controller and invokes "DisplayCustomer" action.This is defined by adding an entry in to the "routes" collection using the "maproute" function. Below is the under lined code which shows how the URL structure and mapping with controller and action is defined.



Where is the route mapping code written?

The route mapping code is written in "RouteConfig.cs" file and registered using "global.asax" application start event.

Can we map multiple URL's to the same action?

Yes , you can , you just need to make two entries with different key names and specify the same controller and action.

Explain attribute based routing in MVC?


This is a feature introduced in MVC 5. By using the "Route" attribute we can define the URL structure. For example in the below code we have decorated the "GotoAbout" action with the route attribute. The route attribute says that the "GotoAbout" can be invoked using the URL structure "Users/about".



What is the advantage of defining route structures in the code?

Most of the time developers code in the action methods. Developers can see the URL structure right upfront rather than going to the "routeconfig.cs" and see the lengthy codes. For instance in the below code the developer can see right upfront that the "GotoAbout" action can be invoked by four different URL structure.


This is much user friendly as compared to scrolling through the "routeconfig.cs" file and going through the length line of code to figure out which URL structure is mapped to which action




From here you can download 100 ASP.NET MVC interview Ebook from this link http://www.codeproject.com/Articles/556995/MVC-interview-questions-with-answers

Shiv Prasad Koirala

Visit us @ www.questpond.com or call us at 022-66752917... read more

We are on Social