ASP.NET MVC Interview Questions and Answers (162) - Page 2

Is the route {controller}{action}/{id} a valid route definition or not and why ?

{controller}{action}/{id} is not a valid route definition.
The reason is that, it has no literal value or delimiter between the placeholders.
If there is no literal, the routing cannot determine where to seperate the value for the controller placceholder from the value for the action placeholder.
Explain about default route, {resource}.axd/{*pathInfo} ...

With the help of this default route {resource}.axd/{*pathInfo}, you can prevent requests for the web resources files such as WebResource.axd or ScriptResource.axd from being passed to a controller.
Explain the difference between adding routes, to a web application and to an mvc application ?

We use MapPageRoute() method of a RouteCollection class for adding routes to a webforms application, whereas MapRoute() method is used to add routes to an MVC application.
Is there any way to handle variable number of segments in a route definition ?

You can handle variable number of segments in a route definition by using a route with a catch-all parameter.
Example:

controller/{action}/{*parametervalues}

Here * reffers to catch-all parameter.
How do you add constraints to a route ?

There are 2 ways for adding constraints to a route. They are:
i) By using Regular Expressions and
ii) By using an object which implements IRouteConstraint interface.
Explain with examples for scenarios when routing is not applied ?

Below are the 2 scenarios where routing is not applied.

i) A Physical File is Found that Matches the URL Pattern - This default behaviour can be overriden by setting the RouteExistingFiles property of the RouteCollection object to true.
ii) Routing Is Explicitly Disabled for a URL Pattern - By using the RouteCollection.Ignore() method, you can prevent routing from handling certain requests.
Explain the usage of action filters in an MVC application ?

Action filter in an mvc application is used to perform some additional processing, such as providing extra data to the action method, inspecting the return value, or canceling execution of the action method.
Action filter is an attribute that implements the abstract FilterAttribute class.
In an MVC application, which filter executes first and which is the last ?

As there are different types of filters in an MVC application, the Authorization filter is the one which executes first and Exeption filters executes in the end.
Difference between Viewbag and Viewdata in ASP.NET MVC ?

Both are used to pass the data from controllers to views.
The difference between Viewbag and Viewdata in ASP.NET MVC is explained below:

View Data:
In this, objects are accessible using strings as keys.

Example:

In the Controller:
public ActionResult Index()
{
var softwareDevelopers = new List<string>
{
"Brendan Enrick",
"Kevin Kuebler",
"Todd Ropog"
};
ViewData["softwareDevelopers"] = softwareDevelopers;
return View();
}

In the View:
<ul>
@foreach (var developer in (List<string>)ViewData["softwareDevelopers"])
{
<li>
@developer
</li>
}
</ul>

An important note is that when we go to use out object on the view that we have to cast it since the ViewData is storing everything as object.

View Bag:
It will allow the objectto dynamically have the properties add to it.

Example:

In the Controller:
public ActionResult Index()
{
var softwareDevelopers = new List<string>
{
"Brendan Enrick",
"Kevin Kuebler",
"Todd Ropog"
};
ViewBag.softwareDevelopers = softwareDevelopers;
return View();
}

In the View:
<ul>
@foreach (var developer in ViewBag.softwareDevelopers)
{
<li>
@developer
</li>
}
</ul>

An important point to note is that there is no need to cast our object when using the ViewBag. This is because the dynamic we used lets us know the type.
What are the file extensions for razor views ?

There are two types of file extensions for razor views.
They are:

i) .cshtml : This file extension is used, when the programming language is a C#.
ii) .vbhtml : This file extension is used, when the programming language is a VB.
How can you specify comments using razor syntax ?

In razor syntax, we use the below shown symbols for specifying comments.

i) For indicating the begining of a comment, we use @* syntax.
ii) For indicating the end of a comment, we use *@ syntax.
Explain the usage of validation helpers in ASP.NET MVC ?

In ASP.NET MVC, validation helpers are used to show the validation error messages in a view.
The Html.ValidationMessage() and Html.ValidationSummary() helpers are used in the automatically generated Edit and Create views by the ASP.NET MVC.
To create a view, the following steps are to be followed:

i) In the product controller,right click on the Create() action and then select the option Add View.
ii) Now check the checkbox labeled in the Add View dialog and create a strongly-typed view.
iii) Select the product class from the View data class dropdownlist.
iv) Now, select Create from the view content dropdownlist.
v) Finally, click on the Add button.
Is it possible to create a MVC Unit Test application in Visual Studio Standard ?

No, It is not possible to create a MVC Unit Test application in Visual Studio Standard.
It is also not possible to create in Visual Studio Express.
It is possible to create only in Visual Studio Professional and Visual Studio Enterprise Versions.
What is Glimpse ?

It is an Open Source platform for gathers and presents detailed diagnostic information about the execution of your web application.It tells you exactly what's going on with each request.

We used in a similar manner to client side diagnostics and debugging tools like FireBug or Chrome Developer Tools.

It is used to reveal what is happening within your ASP.NET MVC and WebForms sites.
What are the HTML attributes that have to specify in @Html.BeginForm()?

NOTE: This is objective type question, Please click question title for correct answer.
What is the difference between Session and TempData in MVC?

NOTE: This is objective type question, Please click question title for correct answer.
How to enable JQuery intellisense support in MVC?

By default, visual studio doesn't provide Jquery intellisense support, but it can be achieved by adding vsdoc.js file to razor view.

@if(false)
{
<script src="~/script/jquery-1.7.1-vsdoc.js" type="text/javascript"></script>
}

No te : If statement prevents the rendering of script tag into the Html source code and it used only by visual studio for Jquery intellisense support.
Difference between Asp.Net MVC and Webforms?

1. Asp.net Webform follows an event-driven development model whereas Asp.net Mvc follow Model, View, Controller design pattern.

2. Asp.net Webform has server controls whereas Asp.net Mvc has Html helpers for creating UI of application.

3.Asp.net Webforms has state management techniques whereas Asp.net Mvc doesn't have automatic state management techniques.

4. Asp.net Webform has file- based Urls means file name exists in Url whereas Asp.net Mvc has route-based Url.

5. In Asp.net webform, webforms(Aspx) are tightly coupled to code-behind (Aspx.cs) whereas in Asp.net Mvc, Views and logic are kept separately.

6. Asp.net Webform has Master page for consistent look and feel whereas in Asp.net Mvc layout is for this.

7. Asp.net Webform has User Controls for code re-usability whereas in Asp.net Mvc partial view is for this.
What is partial view in Asp.Net Mvc?

A partial view is like as user control in Asp.Net web form. Partial views are reusable views like as Header and Footer. It has .csHtml extension.
Partial and RenderPartial Html method renders partial view in Mvc razor.

<div>@Html.Partial("_Comments")</div>
<div>@ { Html.RenderPartial("_Comments") } </div>
What is strongly typed view in MVC?

Strongly type view is used for rendering specific type of model object. By specifying the type of data we get intellisense for that model class. View inherits from Viewpage whereas strongly typed view inherits from Viewpage<T> where T is type of the model.
Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Interview Questions and Answers Categories