7. What is Routing in ASP.NET MVC?
Answer:
The ASP.NET Routing module is responsible for mapping incoming browser requests to particular MVC controller actions. Routing is a pattern matching system that monitors the incoming request and figures out what to do with that request.
Routing helps us to define a URL structure and map the URL with the controller.
Below is the code which shows how the URL structure, mapping with controller and action is defined.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
8. Explain attributes based routing in MVC. How to define it?
Answer:
This feature introduces in MVC 5. By using the "Route" attribute we can define URL structure. Attribute routing provides you more control over the URIs by defining routes directly on actions and controllers in your ASP.NET MVC application and WEB API.
For example in the below code we have decorated the "GotoContact" action with the route attribute. The route attribute says that the "GotoContact" can be invoked using the URL structure "Users/contact".
public class HomeController : Controller
{
[Route("Users/contact")]
public ActionResult GotoContact()
{
return View();
}
}
9. How to enable attribute routing in ASP.NET MVC?
Answer:
To enable attribute routing in ASP.NET MVC5 application, we just add a call to routes.MapMvcAttributeRoutes() method within RegisterRoutes() method of RouteConfig.cs file.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//enabling attribute routing
routes.MapMvcAttributeRoutes();
}
}
10. What is the difference between Routing and URL rewriting?
Answer:
Routing is focused on mapping a URL to a resource while URL rewriting is focused on mapping one URL (new URL) to another URL (old URL).
URL rewriting rewrites your old URL to new one while routing never rewrites your old URL to new one but it maps to the original route.
11. How can we maintain session in MVC?
Answer:
Sessions can be maintained in MVC by three ways:
12. What is the difference between ViewData, ViewBag and TempData?
Answer:
ViewData:
ViewData helps to maintain data when we move from controller to view. It is derived from the ViewDataDictionary class. It is available for the current request only and also requires typecasting for complex data.
ViewBag:
It also helps to maintain data when we move from controller to view. It uses dynamic keyword internally. It is also available for the current request only and typecasting is not required.
TempData:
It is derived from TempDataDictionary class. Tempdata helps to maintain data when we move from one controller to another controller or from one action to another action. It requires typecasting for complex data.