Part - 5 : ASP.NET MVC Interview Question's Answer

21. What is difference between Routing and URL Rewriting?

Many developers compare routing to URL rewriting since both look similar and can be used to make SEO friendly URLs. But both the approaches are very much different. The main difference between routing and url rewriting is given below:
  • URL rewriting is focused on mapping one URL (new url) to another URL (old url) while routing is focused on mapping a URL to a resource.
  • URL rewriting rewrites your old url to new one while routing never rewrite your old url to new one but it map to the original route.

22. What is Route Constraints in ASP.NET MVC?

Route constraints is way to put some validation around the defined route. Creating Route Constraints
Suppose we have defined the following route in our application and you want to restrict the incoming request url with numeric id only.Now let's see how to do it with the help of regular expression.
public static void RegisterRoutes(RouteCollection routes)
 {
   routes.MapRoute(
   "Default", // Route name
   "{controller}/{action}/{id}", // Route Pattern
  new
    {
      controller = "Home",
      action = "Index",
      id = UrlParameter.Optional
    } // Default values for parameters
     );
 }

Restrict to numeric id only
public static void RegisterRoutes(RouteCollection routes)
 {
   routes.MapRoute( 
   "Default", // Route name
   "{controller}/{action}/{id}", // Route Pattern
   new
     {
       controller = "Home",
       action = "Index",
       id = UrlParameter.Optional
       }, // Default values for parameters
       new { id = @"\d+" } //Restriction for id
       );
 }
Now for this route, routing engine will consider only those URLs which have only numeric id like as http://example.com/Admin/Product/1 else it will considers that url is not matched with this route.

23. How route table is created in ASP.NET MVC?

When an MVC application first starts, the Application_Start() method in global.asax is called. This method calls the RegisterRoutes() method. The RegisterRoutes() method creates the route table for MVC application.

24. What are important namespaces in ASP.NET MVC?

There are some important namespaces as given below:
  • System.Web.Mvc - This namespace contains classes and interfaces that support the MVC pattern for ASP.NET Web applications. This namespace includes classes that represent controllers, controller factories, action results, views, partial views, and model binders.
  • System.Web.Mvc.Ajax - This namespace contains classes that supports Ajax scripting in an ASP.NET MVC application. The namespace includes support for Ajax scripts and Ajax option settings as well.
  • System.Web.Mvc.Html – This namespace contains classes that help render HTML controls in an MVC application. This namespace includes classes that support forms, input controls, links, partial views, and validation.

25. What is View Engine?

A View Engine is a MVC subsystem which has its own markup syntax. It is responsible for converting serverside template into HTML markup and rendering it to the browser. Initially, ASP.NET MVC ships with one view engine, web forms (ASPX) and from ASP.NET MVC3 a new view engine, Razor is introduced. With ASP.NET MVC, you can also use other view engines like Spark, NHaml etc.

0 Komentar untuk "Part - 5 : ASP.NET MVC Interview Question's Answer"

Back To Top