ASP.NET MVC Interview Questions with Answers Part2

ASP.NET MVC is a Framework based on MVC pattern and developed by Microsoft Corporation. This framework is very strong, user friendly and has great features to develop and maintains large scale applications. This article explains some important questions and answers in ASP.NET MVC. Hope it will help you to build successful carrier.

What are the different types of action results in MVC?
In MVC there are 12 results. Among them ActionResult class is a base class for all action results. The 11 sub classes are given bellow:

ViewResult – renders a view as a web page to the response stream

PartialViewResult – renders a view inside another view

EmptyResult – returns a empty or null result

RedirectResult – redirect to another action method (HTTP redirection)

RedirectToRouteResult – redirect to another action method (HTTP redirection) that is determined by the routing engine, based on a given route

JsonResult – returns a JSON object form a ViewData

JavaScriptResult – returns a script code that can be executed on the client

ContentResult – returns a user-defined content type

FileContentResult – returns a file to the client

FileStreamResult – returns a file to the client, which is supplied by a Stream

FilePathResult – returns a file to the client

How to implement Windows authentication in MVC?
For Windows authentication, at first we need to change the web.config file and set the authentication mode to Windows. A sample code for that is given bellow:

Then in the controller or on the action, we can use the Authorize attribute which specifies which users have access to these controllers and actions. A sample code for that is given bellow, where only the user “Administrator” can access it.
[Authorize(Users= @”WIN-3LI600MWLQN\Administrator”)]

How to implement Forms authentication in MVC?
For Windows authentication, at first we need to change the web.config file and set the authentication mode to Forms. Here the login URL points to a controller rather than a page. A sample code for that is given bellow:

What do you know about bundling and minification in MVC?
In MVC, bundling and minification helps us improve request load times of a page. It increases the performance of the applications.

What is bundling and how to implement bundling in MVC?
In a project we need to use CSS and script files. Bundling helps us to combine multiple JavaScript and CSS files into a single file. Thus it minimizes the multiple requests in to a single request.
In order to implement bundling open the BundleConfig.cs file from the App_Start folder. Write the following codes which will combine all the JS files of Scripts folder into a single unit.

What is minification and how to implement minification in MVC?
Minification is a process to remove the blank space, comments etc.
The implementation of minification is as like as bundling. That means when we implement bundling minification is also implemented automatically. A sample JavaScript codes with comments:
// This is test comments
var x = 5;
x = x + 2;
x = x * 3;

What is an area in MVC?
In MVC, areas help us to organize all the functionalities into independent modules. For a big project which has 100’s of controller classes, it is really difficult to manage them. Areas help us to groups all the classes based on their functionality.

How to implement are in MVC
We can add area by right clicking on the solution and clicking on “Area”. Give an appropriate name for the area. On the solution explorer we will find Controllers, Models, and Views.

How can you handle multiple Submit buttons in MVC?
Consider we have two submit buttons.
We can handle this problem in two ways. First one is normal HTML way and second one is Ajax way.
For HTML way, we need to create two forms and place the submit button inside each of the forms. Sample code is given bellow:

What is Scaffolding in MVC?
Scaffolding is a technique in which MVC template helps us to generate codes for CRUD operations. During controller creations we need to select
…Controller with read/write actions
…Controller with views, using Entity Framework

In MVC what does scaffolding use internally to connect to database?
Scaffolding uses Entity Framework internally to connect to database.

What is a model binder in MVC?
In MVC, model binder acts like a bridge between HTML UI and MVC model. Sometimes the name of HTML UI is different than the model property names. For that case we can write the mapping logic in the binder between the UI and the model.

How can we use multiple models in a single view?
When we bind a model with a view we can select only one model. But if we need to use multiple models classes in single views, we need to create a new model class which aggregates all the models.

In MVC (Model, View & Controller) which one is best to create first?
It is best to create Model.

Can we create automatically generate the codes for CRUD operations?
Yes, if we define Model first we can generate actions automatically.

What are the helper classes?
Instead of writing Input tag you take help of Helper classes. For your understanding these are alike ASP.NET Server controls. However these have there are lot differences between ASP.NET webforms server controls like Textbox, Dropdown etc..

Can we access Model in cshtml page?
Yes, we can access Models in cshtml Page or views.

Can we share a view across multiple controllers?
Yes, if we put a view into the shared folder.

How to avoid XSS vulnerabilities in MVC?
To avoid XSS vulnerabilities we need to use the syntax ‘<%: %>’ instead of syntax ‘<%= %>’. It is applicable in .Net Framework 4.0. It does the HTML encoding. A sample code is given bellow:

About Author

Rashed

I’m Rashedul Alam, software engineer/architect, technical blogger, and educator. I have over 10 years of working experience in the field of software design and development. I like to share my knowledge and technical stuff with others. I am a community contributor, providing problem solving answer through different forum.