x

Wednesday, January 27, 2016

ASP.NET MVC Interview Questions and Answers

ASP.NET MVC Interview Questions and Answers

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. We hope you find these questions useful.

1.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.

2.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.

3.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:

Also we need to create a controller where we will check if the user is valid or not. If the user is valid we will set the cookie value. A sample C# code for that is given bellow:

We need to use Authorize attribute so that any unauthorized user can’t call to these controllers. Sample C# code is given bellow:

4.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.

5.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

6.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;

After implementing minification the JavaScript code looks like:
var x=5;x=x+12x=x*3

7.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

8.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:

In Ajax way, we can create two different functions (“Fun1” and “Fun1”). We need to bundle each of two functions on button click event. Sample code is given bellow:

9.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.

10.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.

11.Is MVC 4 support Windows Azure SDK?

Yes, it supports.

12.Why we need display mode in MVC?

Display Modes is newly feature added in ASP.NET MVC 4. It helps us to change the view automatically depending on the user devices (desktop, smart phone, etc).

13.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.

14.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.

15.In MVC what does scaffolding use internally to connect to database?

Scaffolding uses Entity Framework internally to connect to database.

16.Can we access Model in cshtml page?

Yes, we can access Models in cshtml Page or views.

17.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..

18.Can we create automatically generate the codes for CRUD operations?

Yes, if we define Model first we can generate actions automatically.

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

It is best to create Model.

20.It is best to create Model.

System.Web.Mvc assembly

21.What is MVC (Model View Controller)?

The is MVC (Model View Controller) is a design pattern used in software engineering. These patterns propose there components or object to be used in software development:

  • Models – data access layer. This can be direct data access, web services, etc
  • Views – presentation layer of the application. This the users interface.
  • Controllers – business logic for the application. It controls Models & View.

22.What are the Development Models in ASP.NET?

ASP.NET supports three different development models. Such:

  1. Web Pages- It is a Simplest ASP.NET model. It is Similar to PHP and classic ASP.
  2. MVC (Model View Controller)- MVC separates web applications into 3 different components: Models for data,Views for display,Controllers for input.
  3. Web Forms -The traditional ASP.NET event driven development model.

23.What is ASP.NET?

ASP.NET is a framework for building web pages and web sites with HTML, CSS, JavaScript and server scripting. It follows object oriented programming approach. It is not a programming language. If we write code in note pad and run it we will get output. It is a server side technology. It uses all .NET compatible language such as C#, VB.NET, J# etc. If we write our code in any language after compilation .NET convert it into MSIL.

24.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:

25.Can we share a view across multiple controllers?

Yes, if we put a view into the shared folder.

26.What are the Core features of ASP.NET MVC?

Clear separation of application Presentation and Business Logic layer
An extensible and pluggable framework
Extensive support for ASP.NET Routing
Support for existing ASP.NET features

27.Is MVC different from a three layered architecture?

MVC is an evolution of traditional three layer architecture. Many components of the three layered architecture are part of MVC.
Look and Feel- [Three layered architecture] User interface [MVC] View
UI logic- [Three layered architecture] User interface [MVC] Controller
Business logic /validations- [Three layered architecture] Middle layer [MVC] Model
Request is first sent to- [Three layered architecture] User interface [MVC] Controller
Accessing data- [Three layered architecture] Data access layer [MVC] Model

28.Difference between ASP.NET WebForms and ASP.NET MVC?

ASP.NET WebForms

  1. It uses Page controller pattern approach for rendering layout. That means every page has its own controller (code-behind page)
  2. Page size is big. Because it has large viewstate
  3. Testing is difficult.
  4. It is good for small scale of application and limited team size.

ASP.NET MVC

  1. It uses Front Controller approach. That means a common controller for all pages.
  2. Page size is small. Because, it has no viewstate.
  3. Testing is easy.
  4. It is good for large scale of application with large team size.

29.What is ASP.NET MVC?

ASP.NET MVC is a web development framework developed by Microsoft Corporation. It is based on MVC (Model-View-Controller) architectural design pattern.

30.History of MVC
For the first time MVC Architecture was implemented by Trygve Reenskaug at 1979. It was implemented on Smalltalk at Xerox labs. The coders and software engineers accept the benefits and advantages of this architecture.

31.Explain the complete flow of MVC?

All clients or end user requests are first sent to the controller. Depending on the request the controller decides which model will use to fulfill the request. The controllers load the model and generate the appropriate view. The final view is sent to the end user on the browser.

32.What is Razor in MVC?

Razor is a light weight view engine. Before MVC 3 there was only one view type (ASPX). Razor was introduced in MVC 3.

33.How can we do authentication and authorization in MVC?

We can use Windows or Forms authentication for MVC.

34.What are the benefits of using MVC?

By using MVC we can get some benefits:
Separation of layer. If we write our code in a class file instead of code-behind page we can reuse the same code.
Page size is small. Because, it has no viewstate.
Testing is easy. Automated UI testing is possible.

35.Is MVC suitable for both Windows and Web applications?

The MVC architecture is suitable for a web application than Windows. For Window applications MVP (Model View Presenter) is more appropriate. If you are using WPF and Silverlight then MVVM is more suitable due to bindings.

36.Can we create our custom view engine using MVC?

Yes.

37.What is Routing in ASP.NET MVC?

In typical ASP.NET application all the incoming requests are mapped to physical files such as .aspx file. ASP.NET MVC framework uses friendly URLs that are not mapped to physical files. It uses a routing engine that maps URLs to controller. We can define our routing rules for the engine so that it can map incoming request URLs to appropriate controller.
In a browser for an ASP.NET MVC application when a user type a URL and presses “go” button, routing engine uses routing rules that are defined in Global.asax file and find out the path of corresponding controller.

38.How to implement AJAX in MVC?

We can implement AJAX in MVC in two ways:

  • AJAX libraries
  • jQuery

39.So which is a better fit, Razor or ASPX?

As per Microsoft, Razor is more preferred. Because it’s light weight and has simple syntaxes.

40.Why Razor when we already have ASPX?

Razor is clean, lightweight, and syntaxes are easy as compared to ASPX.
For example in ASPX to display time we need to write:
<%=DateTime.Now%>
But in Razor the code is simple:
@DateTime.Now

41.Give an example of ViewBag & ViewData

An example of ViewBag  and ViewData is given bellow:

42.What are ViewData, ViewBag and TempData?

ASP.NET MVC provides 3 options (ViewData, VieBag and TempData) for passing data from controller to view and in next subsequent request. Among them ViewData and ViewBag are almost similar and TempData performs additional responsibility.
ViewData and ViewBag are used to communicate between controller and corresponding view.
TempData allows for persisting information for the duration of a single subsequent request. TempData is also a dictionary object. It stays for the time of an HTTP Request. It can be used to maintain data between redirects (from one controller to the other controller).
ViewData is a dictionary of objects that is derived from ViewDataDictionary class and accessible using strings as keys.
ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
ViewData requires typecasting for complex data type and check for null values to avoid error.
ViewBag doesn’t require typecasting for complex data type.

43.How can we navigate from one view to another using a hyperlink?

We can do it by using the ActionLink method. Here is a sample code that navigates the “Home” controller and invokes the GotoHome action.

44.Can we map multiple URL’s to the same action?

Yes, we can. We just need to make two entries with different key names and specify the same controller and action.

45.Where is the route mapping code written?

It is written in the global.asax file.

46.What is HTML helper in MVC?

The HTML helper helps us to render the HTML controls in the view. The code to display a HTML textbox on the view is given bellow:

47.What is the latest version of ASP.Net MVC?

The latest version of ASP.NET MVC is 6.

48.Is MVC appropriate for both Windows and Web applications?

The MVC architecture is suitable for a web application than Windows application. For Window applications, MVP (Model View Presenter) is more applicable. For using WPF and Silverlight MVVM is more suitable.

49.What is the difference between ActionResult and ViewResult?

ActionResult is an abstract class. ViewResult derives from the ActionResult. ActionResult has several derived classes (ViewResult, JsonResult, FileStreamResult).
ActionResult can be used to exploit polymorphism. So if you want to return different types of views dynamically then ActionResult is the best thing.

50.TempData Example

Sample code is given bellow:

We have given the top 50 Important interview questions with answers. Looking forward to hearing from you if you have any other questions or require any further assistance. Please feel free to mail us atntp.interview@gmail.com if you have questions or comments. Visit site at http://www.interviewquiz.net/for updates and more information.

 

Leave a Reply

1 comment:

  1. Hello,


    I’ve often thought about this Interview question and answer. Nice to have it laid out so clearly. Great eye opener.

    I have this task to restructure and clean huge amounts of code, including removal of junk code, changing functions/variables names to adhere to naming conventions and similar changes.
    By the way do you have any YouTube videos, would love to watch it. I would like to connect you on LinkedIn, great to have experts like you in my connection (In case, if you don’t have any issues).


    Cheers,
    John

    ReplyDelete