Click here to Skip to main content
15,064,485 members
Articles / Web Development / ASP.NET
Article
Posted 25 Jul 2021

Stats

3.9K views
6 bookmarked

ASP.NET MVC Interview Questions with Answers

Rate me:
Please Sign up or sign in to vote.
2.04/5 (8 votes)
25 Jul 2021CPOL12 min read
60 plus most asked ASP.NET MVC Core interview questions with answers
In this article, we will run through 60 plus ASP.NET MVC Interview questions with answers. These ASP.NET Interview questions are around Dependency injection, Session Management, WEBAPI and so on. This article nowhere undermines that you can get job by just reading these questions. This is a last minute revision article and not a short cut to clear ASP.NET Interviews. Happy learning, happy job hunting.

In case you are not in a mood to read this long article, you can watch the below ASP.NET Interview questions with answers video which covers all questions with answers and small demonstrations.

Image 1

Let's Be Focused, Define Your ASP.NET Preparation Scope

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.

Image 2

Now if you are a junior / fresher, I would say stick to MVC Core and say you have just worked with MVC core, so that you do not get Webforms and MVC questions .

But if you are a senior, you do not have too many choices. You will need to prepare 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 to say all of them during interviews, it's 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 in bold italics.

  Webforms (2003) ASP.NET MVC 5 (2009) ASP.NET MVC Core (2017)
Cross platform No No No Yes
Performance Worst due to view state and page life cycle. Less than ASP.NET Core Best of all the versions
Simplification Quite complicated Mediumly complicates Very simplified
Cloud ready Not meant for cloud Not complete Yes
HTML control Very less Good control due to Razor view Good control due to Razor view
Configuration Web.config Web.config Appsetting.json
Dependency Injection Need third party Need third party Built in
Self-hosting No No Yes (Kestrel)
Static content No No such folder wwwroot
Nuget Managed Not Fully Not fully Completely Nuget managed
Drag and Drop Rad Full marks Need to code Need to code.
Development Only windows Only windows You can develop on Mac, Linux using VS Code.

Can you Explain MVC Architecture?

Image 3

MVC is an architecture pattern where we divide project into 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 the 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 Middleware.

Explain the Various Ways of Doing DI in MVC?

This is an easy topic to understand but very difficult to explain to the 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, the same instance will be injected for every dependency injection. So “obj” and “obj1” will have the same instance injected.

In case of singleton, one big global instance is created. For all request, for all Dependency injection, same instance will be injected.

Image 4

What are the Various Ways of Doing Session Management in ASP.NET Core?

  • Session variables
  • Tempdata
  • ViewData/ViewBag

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 cannot satisfy each one of them. So kestrel acts like a in-process web server, takes a request, sends to MVC core application, gets response and sends it back to the main web server.

Explain the 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 for 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:

  • ViewData/ViewBag
  • Session variables
  • Tempdata

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, the server treats it as a new request.

What are the Various Ways of Doing Session Management?

There are three primary ways of doing session management, Session variables , viewdata/viewbag and Tempdata.

Are Sessions Enabled by Default?

To keep ASP.NET lightweight, 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 Mean 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 persist. Viewdata scope only from action to view.

ViewBag vs ViewData?

ViewBag is syntactic sugar over viewdata.

How does ViewBag Work Internally?

ViewBag uses dynamic variables.

Explain viewModels?

They represent data for a view.

ViewBag vs ViewModel - What's the Best Practice?

For passing data from action to view, use viewmodel as ViewBag is dynamic and we can have mistakes.

Explain tempdata?

Tempdata persists for the current and we can control whether 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 persists 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 persists across action redirect and also we can decide using keep, can we persist 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 persist in action or controller redirects.
  • Viewbag is a syntactic 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 the 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 is Different from MVC Controller?

In MVC controller, we can return RAZOR views but in WebAPI, we cannot 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 created 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 use WCF REST.

How to Return HTTP Status Codes?

JavaScript
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 using JS frameworks like Angular, React and they communicate to MVC using HTTP.

Further Questions for Self Learning

  • What's 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 custom taghelper?
  • How to create our own custom htmlhelper?
  • Ways of implementing versioning in ASP.NET Core API?
  • .csproj file, and its 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 the 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 middleware?
  • Authentication in ASP.NET core web API and OWIN?
  • 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?

History

  • 26th July, 2021: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


Comments and Discussions

 
QuestionGarbage Pin
Dave Kreskowiak26-Jul-21 9:15
mveDave Kreskowiak26-Jul-21 9:15 
AnswerRe: Garbage Pin
Shivprasad koirala26-Jul-21 11:37
MemberShivprasad koirala26-Jul-21 11:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.