Wednesday, August 19, 2020

ASP.Net MVC Interview Questions and Answers

I am listing down all questions which usually i ask in the ASP.Net MVC interview and i been asked but answer is not in deep, if you want to learn in details please contact me.

1. What is MVC design pattern?

MVC design pattern is an architectural design pattern for the web UI layer.
This architecture separate the dynamic web application in three components as below:

Model: Model objects are the parts of the application that implement the logic for the application's data domain.

View: Views are the components that display the application's user interface (UI). Typically, this UI is created from the model data.

Controller: Controllers are the components that handle user interaction, work with the model, and ultimately select a view to render that displays UI.

2. What is the advantages of ASP.Net MVC design pattern?

I. This is light weight compare to ASP.Net event driven.
II. UI(view), data logic (model) and events (controllers) by design get separated, this way SoC design principle implemented.
III. Independent unit test can be written for all the MVC components.
IV. As components get separated out so best resource can work on the different components.

3. Explain page life cycle of ASP.Net MVC design pattern?

I. When the request is first time then then RouteTable class initiated with the route data.
II. The UrlRoutingModule find the match of URL from the RouteData of RouteTable and instantiates a MVCHandler which is HttpHandler
III. Request send to MvcHandler and this will create the controller class object
IV. Controller class combine the data and view then sent the response to the browser

4. Which version of ASP.Net MVC you worked? Brief history or the version? Explain few latest features.

Interviewer just want to confuse you here..just be prepared for version no. Here is the history of the ASP.Net MVC version:

5. Can you please explain how routing works? Explain the URL Patterns.

Please refer Q. 3.
URL patterns works as below:
Please refer the below image and follow the statements:
I. "Home" is the controller name
II. "Index" is the action method name
III. If URL "http://localhost:52845" anyone type then invoke the default URL as per the route table

Can can find the RegisterRoutes code in the "App_Start\RouteConfig.cs"

6. Can we write the custom route ? if yes then how?

Yes, we can write the custom route and we can write in the file "App_Start\RouteConfig.cs".

Can can find below "http://localhost://52845/Hotel/Index" is from custom route.

7. What is the difference between TempData, ViewData, and ViewBag?

ViewData and ViewBag: 
ViewData and ViewBag both use to pass data from controller to view and data will be available for the current request only. Only the difference  is ViewData is dictionary object and ViewBag is dynamic property.

TempData also a dictionary object and this hold the data till the HTTP request time this mean we can use this data when redirect from controller to controller action methods and data will be persist.

8. What is Partial View in ASP.Net MVC?

Using multiple partial view to make a page or we can say re-usable view component.

9. What is area View in ASP.Net MVC?

Divide your application area wise this mean if large application then divide then into area so then better code management. e.g. admin modules can create as different area.

10. What is the difference between model and ViewModel?

Model and ViewModel is same to hold the data only the diffreance is ViewModel is custom model for the view.

11. What is filter? How many different type of filter available? Explain all.

Filter is execution of some logic before and after execution of action methods. There are four different type of filters as below:
I. Authorization filters – For authentication and authorizes.
II. Action filters – Before and after action methods execute. i.e. Authorize, OutputCashe, ValidateInput, HandleError
III. Result filters – Before and after view result.
IV. Exception filters – If some unhandled exception

12. How do you implement the unit testing in the ASP.Net MVC?

I. No dependency object been created in the controller class.
II. Implement dependency injection by Unity framework

13. Explain the different return types of an action method?

Below are the different type of return types:

14. How to overload an action method?

By ActionName filter you can overload the methods.

        public ActionResult Contact()
            ViewBag.Message = "Your contact page.";

            return View();

15. What is Razor in ASP.Net MVC?

Razar is open source view. In MVC you can implement ASPX view also.

16. How you will do Caching in ASP.Net MVC?

By OutputCache filter we can implement caching in the MVC.

Sample code :

        [OutputCache(Duration = 60)]
        public ActionResult Index()
            return View();

17. How you will pass data from controller to controller action method?

By TempData

18. Can you please explain some features of MVC 4?

Please go though the my blog ASP.Net MVC 4 Features

No comments:

Post a Comment

Azure Function or Serverless Function

In this blog I covered what is Azure function? Why to use azure function? How to create azure function from azure portal and how to pass pa...