60 + important ASP.NET MVC Interview Questions with answers
In this article we will run through some very important ASP.NET Interview Questions with answers. Do comment, so that it can reach to those developers who are not having jobs or looking for a change.
Also you watch the below youtube video which is a 1 hour of video and run through 25 important ASP.NET Interview questions.
Lets plan before we start preparation
When you talk about asp.net interviews the range of questions can be very narrow or it can be very wide. So do ensure you have proper preparation strategy at place. Remember ASP.NET has three versions of framework. So we had ASP.NET Webforms which came in 2003, followed by the ASP.NET MVC , then we had ASP.NET MVC Core.
Now if you are junior / fresher I would say stick to MVC Core and say you have just worked with MVC core , so that you do not get the questions of Webforms and MVC.
But if you are senior you do not have too many choices you will need to prepare through right from Webforms to MVC Core.
?What is ASP.NET MVC Core ?
?ASP.NET MVC Core is an open source, cross platform framework to develop web applications.?When you develop web applications using ASP.NET Core it can run on windows, Linux and mac.
?Can you explain the difference between MVC core vs MVC 5 vs Webforms?
?Now there are like 10 to 12 differences and if we try say all of them during interviews its not going to work. Always remember let the interviewer speak more and you as a candidate should speak to the point.
I have listed below all 12 points but would recommend to start with the first 4 points which are highlighted.
? ?
Can you explain MVC Architecture?
MVC is an architecture pattern where we divide project in to three layers.
The first layer is the view which has things like Color, UI positioning, CSS, input controls, buttons and so on.
Second layer is Model which is a simple class which has business validations.
Controller binds the model and the view.
So the first hit comes to the controller, it loads the model and binds the model data to the view.
Where do we store configuration in ASP.NET Core ?
Configuration data is stored in a json file called as Appsettings.json .
?How can we read configuration file in ASP.NET Core ?
To read configuration from appsettings.json you need to use “IConfiguration” interface
?What is dependency injection?
?Dependency injection is practice of providing dependent objects for a class from outside rather than the class creating it. So, in simple words the object creation process is isolated from the caller. This helps to create a proper decoupled system.?
?Why do we need dependency injection?
By using DI classes are decoupled from each other so you make changes at one place its reflected all over the places.
How to implement dependency injection?
To implement DI we need to add the service in “ConfigureServices” method which is in Startup.cs file.
Explain the concept of middleware?
Middleware helps to add pre-processing logic before the request is sent to the controller.
How to implement middleware in MVC Core ?
Middlewares are added in “Configure” method of Startup.cs file.
Explain the importance of Startup.cs file in ASP.NET Core ?
?Startup.cs file helps to configure Dependency injection and Middle wares.
?Explain the various way of doing DI in MVC?
?This is an easy topic to understand but very difficult to explain to interviewer in simple words. So, I would suggest to go through the below explanation, understand it and create your simple one liner.
?There are three ways of doing DI: - Scoped, Transient and Singleton.
Transient: - Creates new instances, every single time service is injected.
Scoped: - Creates new instances, once per request made to server.
Singleton: - Instantiates one global object for all requests coming to server from any user.
The below image shows the same visually. In transient for every dependency injection new instance will be injected. For example, in the below image for “obj” and “obj1” fresh new instances will be injected for every request.
In case of scoped for every request same instance will be injected for every dependency injection. So “obj” and “obj1” will have same instance injected.
In case of singleton one big global instance created. For all request , for all Dependency injection same instance will be injected.
What are the various ways of doing session management in ASP.Net Core ?
?ConfigureServices vs ?Configure method ?
?In ConfigureServices we add the objects to be dependency injected while in Configure method we specify middlewares
?Explain Razor pages ?
Razor is a view engine in which you can write both C# and HTML code. C# code is rendered at the server side.
?How can we pass model data?to the view ?
You can pass by using viewdata or using strongly typed views.
What is a strongly typed view ?
Strongly typed views are?Razor views where we get intellisense for the model used in the view.
Explain the concept of viewModel ?
View Model represents the data to be displayed on the view.
Explain kestrel web server ?
Kestrel is an open source default web server used by ASP.NET core application. Kestrel ships with ASP.NET Core setup and works as in-process web server to handle web request. Kestrel is cross platform it works on Linux , windows?and Mac.
Every request of ASP.NET core goes through kestrel first.
?Why kestrel web server when we have IIS ?
?ASP.NET core is meant to run cross platform. So, IIS as default server will not work , because IIS works only on Windows. Web server like NGINX , Apache and so on have their own way to running the application startup and ASP.NET core can not satisfy each one of them.?So kestrel acts like a in-process web server take a request send to MVC core application, get response and sends it back to the main web server.
Explain concept of reverse proxy ?
?Reverse proxy is a concept where the server acts like a mediator. Kestrel works on reverse proxy concept it takes the request from the main web server like IIS / Apache and forwards its to MVC application and vice-versa.
?What are cookies ?
Cookies are small text file where browser can store user related information.
?What is the need session management ?
HTTP protocol is stateless to maintain states we need to use session management.
?What are the various ways of doing Session management in ASP.NET ?
?There three ways of doing session management: -
?What exactly is a session ?
?Session is user interaction which happens with a website over a period of time.Its interaction which happens right when browser opens and browser closes.
?Explain "HTTP is a stateless protocol" ?
?HTTP does not remember states between request and response. User sends a request , server processes it and sends response. Now if the user comes again in the same session server treats its as a new request.
?What are various way of doing session management ?
?There are three primary ways of doing session management Session variables , viewdata/view bag and Tempdata.
?Are sessions enabled by default ?
?To keep ASP.NET?light weight session variables are not enabled by default.
?How to enable sessions in MVC core ?
?To enable session variables in Startup.cs we need to "AddSession" and "UseSession".
?Are sessions variables shared(global) between users ?
?Session variables are created for a particular user. Data of session variable is not shared between users.
?Do session variables use cookies ?
Yes,session variables use cookies.
?What is a cookie ?
Cookies are small text files information which is stored in the end users computer.
?Explain idle time out in sessions ?
Idle timeout is the time when user does not do any activity. So if user is inactive you would like to ensure session variables expire.
?What does a Context means in HTTP ?
HttpContext holds information like session , request data and response information. It has all the data needed for a particular request and response.
领英推荐
?When should we use viewdata ?
To pass data from controller to view.
?How to pass data from controller to view ?
By using viewdata.
?In same request can viewdata persist across actions ?
No it will not persiste. Viewdata scope only from action to view.
?ViewBag vs ViewData ?
ViewBag is syntatic sugar over viewdata.
?How does ViewBag work internally ?
ViewBag uses Dynamic variables.
?Explain viewModels ?
They represent data for a view.
?ViewBag vs ViewModel Whats the best practice ??????????????????????
For passing data from action to view use viewmodel as ViewBag are dynamic and we can have mistakes.????
Explain tempdata ?
Tempdata persists for the current and we can control do we want the state in the next request or not.
Can tempdata persist across action redirects ?
Yes
How is tempdata different from viewdata ?
Tempdata can persist from action to action ( i.e. action redirects) while viewdata is only from action to the view.
?If tempdata is read is it available for next request ?
No
?How to persist tempdata ?
We can persist tempdata by calling the keep method.
?What does Keep do in tempdata ?
Keep method persist the data for the next request.
?Explain Peek in tempdata ?
Peek = Keep + Read. It will read as well as keep the data.
?How is tempdata different from session variables ?
Session variables persist right from browser open to browser close.While tempdata persist across action redirect and also we can decide using keep can we perist state for new request.
?If i restart the server does tempdata,session stay ?
No, tempdata and session do not persist in server restarts.
?Is tempdata private to a user ???????
Yes , Tempdata is private to a user.
?ViewData vs ViewBag vs Tempdata vs Session variables
?Session variables scope is from browser open to close.
Viewdata scope is only from action to view.
Viewdata does not persists in action or controller redirects.
Viewbag is a syntatic sugar over ViewData.
Tempdata maintains data in single request irrespective we have redirects or not.
Once tempdata is read state is not maintained across request.
If we call Keep method tempdata state is maintained across requests even if tempdata is read.
?What is?WebAPI ?
Web API controller delivers data and services easily by using HTTP?REST services.
?What is the advantage of WebAPI ?
?HTTP is a most used protocol so by exposing your service over HTTP any nature of clients can consume it , making it a true client server model. So your client can now be a mobile app or app made in JS frameworks like react/Angular?or must be a simple browser , every one can integrate with WebAPI seamlessly.
?Explain REST and Architectural constraints of REST?
?REST stands for representational state transfer. REST is a architectural style/principle where client and server talks in representations and states.
Some important principle of REST is Client Server,Statelessness,Unique URI,Manipulation happens through representation,
WebAPI helps to expose services / data using HTTP protocol.
?Can we use?UDP/TCPIP protocol with Web API?
No
?How WebAPI different from MVC controller ?
In MVC Controller we can return RAZOR views but in WebAPI we can not return Views. WebAPI follows REST principles and does things like content negotiations.
?What is content negotiations in Web API ?
Content negotiations looks at client accept type and depending on the client accept type it provides data format. So if in the accept client sends XML it will send XML , if it sends JSON it will send JSON.
?WebAPI vs WCF ?
WCF was meant for SOA , XML is compulsory format and support any protocol.
WebAPI was meant for REST, No compulsion on Format and supports only HTTP protocol.
?WCF REST vs WebAPI REST ?
WCF REST was creates to ensure that old legacy WCF can be REST enabled , While WebAPI was built from scratch to serve REST architecture.For new project use WebAPI. If you want to make legacy WCF compatible with REST you will used WCF REST.
?How to return HTTP status codes ?
return StatusCode(StatusCodes.Status200OK,"Test");
?For error which status code is returned ?
500 internal server error.
?How did you secure your web API ?
By using JWT token.
?How do current JS frameworks work with webAPI ?
Client side is built usin JS frameworks like Angular , React and they communicate to MVC using HTTP.
?????
Further questions for self learning.
?Whats the importance of Filters ?
Generic Host vs WebHost ?
How can we manage to add and switch environment specific appsetting file?
what is the difference between IApplicationBuilder.Use() and IApplicationBuilder.Run()?
what is the difference between htmlhelper and taghelper?
How to create custom taghelper?
How to create our own custom htmlhelper?
Ways of implementing versioning in Asp.net core Api
.csproj file, and it's importance in .net core. comparison of .csproj file with previous version of .net
?What is the difference between creating .net std library and .net core library?
what is use of UseRouting and UseEndpoints in Startup Configure method
How to store ServiceCollection in different location other than ConfigureServices method
What is ViewComponent? and can we inject Dependency in ViewComponent?
how to use in-memory cache in asp.net core
how to store object in session other than string and int in asp.net core.
how to create CUSTOM MIDDLEWARE in ASP.NET CORE
use of launchsetting.json in ASP.NET Core
how to configure runtime compilation of views while development. :- AddRazorRuntimeCompilation();
Health checks
ASYNCACTIONFILTER IN ASP.NET CORE MVC
How to Secure asp.net core with oAuth2.0 and what is oAuth middle ware
2. Authentication in asp.net core web api and OWIN
3. Explain factory pattern using built - in DI in asp.net core
.csproj file, and it's importance in .net core. comparison of .csproj file with previous version of .net
Ways of implementing versioning in Asp.net core Api
What is IOption in ASP.NET core
Dotnet Developer | Angular | Azure | LWD 15 Jan 25
8 个月This is very precise
Founder of NomadCoder | Creating Job Ready Developers
3 年God here
Company
3 年For a complete course, package do visit https://www.questpond.com/interview-questions---answers-tutorial/cid63 Happy Learning and Job Hunting.
Senior Engineer @ HCL | C# FullStack | Catalyst Cult Member
3 年Sir in document pictorial representation of mvc architecture definition got change for model and controller,
Founder of Questpond. Helping techies to secure jobs.
3 年Do let me know if any answers are wrong or controversial. And if you have any interview question in mind around ASP.NET core please put it here will add to the article. Happy learning happy job hunting.