ASP.NET MVC Interview Questions Part 4

19. What is View Engine?


View Engine is responsible for rendering view into html form to the browser. By default, MVC supports Web Form(ASPX) and Razor View Engine. There are many third party view engines (like Spark, Nhaml etc.) that are also available for MVC. 

For example, ASP.NET comes with its own view engine out of the box. That is the one where views have lots of tags like <% %> and <%: %>. It uses the .aspx file extension.

With ASP.NET MVC3, another out-of-the-box view engine was added, Razor, which has a more appealing syntax, e.g. <div>@Model.UserName</div>.

20. What is Razor in MVC?


The Razor View Engine is an advanced view engine, available with MVC 3.0 and later versions. Razor uses "@" character instead of "<% %>" as used by the ASPX View Engine.

Razor is an advanced view engine and it is compact, expressive and also reduces typing.
@Html.ActionLink("SignUp", "SignUp")

21. What is the difference between Razor and web form engine?


Razor View EngineWeb form view engine
Razor syntax is very compact that reduces typing.It has the syntax which is same as an ASP.Net form application.
The namespace for Razor Engine is System.Web.Razor.The namespace for Webform Engine is System.Web.Mvc.WebFormViewEngine.
Razor view engine uses @ symbol to render server-side content.It uses "<%= %>" or "<%: %>" to render server-side content.
The Razor engine supports test driven development (TDD).Webform view engine does not support test driven development.
Razor has no file dependency.ASPX files have a dependency on the ASP.NET runtime  to parse and execute those ASPX files.
The Razor View Engine prevents Cross Site Scripting (XSS) attacks by encoding the script or HTML tags before rendering to the view.A web form View engine does not prevent Cross Site Scripting (XSS) attack.
Razor engine doesn't support design mode in visual studio.Web form engine supports design mode in visual studio, means we can see look and feel of our page without running the application.

22. How can we perform authentication and authorization in ASP.NET MVC?


We can use both Windows and Form authentication in ASP.NET MVC.

Implement Windows authentication for MVC:
For windows authentication we need to modify the web.config file and set the authentication mode to Windows.

    <authentication mode="Windows"/>
            <deny users="?"/>

Implement Form authentication for MVC:
The first step is to set the authentication mode to "Forms". The loginUrl points to a controller rather than a page.

    <authentication mode="Forms">
        <forms loginUrl="∼/Home/Login"  timeout="20"/>

23. What is Areas in MVC?


ASP.NET MVC 2 introduced Area. Area allows partitioning of large application into smaller units where each unit contains separate MVC folder structure, same as default MVC folder structure.

When you add an area to a project, a route for the area is defined in an AreaRegistration file. The route sends requests to the area based on the request URL.

24. What is ViewStart? and when to use ViewStart?


The _ViewStart.cshtml page is a special view page containing the statement declaration to include the Layout page. Instead of declaring the Layout page in every view page, we can use the _ViewStart page. When a View Page Start is running, the “_ViewStart.cshtml” page will assign the Layout page for it.


    Layout = "∼/Views/Shared/Layout.cshtml";

When to Use:
The _ViewStart.cshtml page is used to serve same layout for group of views. The code within this file is executed before the code in any view placed in the same directory. Thus, a view can override the Layout property and choose a different one.