.NET Core Interview Questions & Answers [2021]

According to Stackoverflow Survey 2020, developers are using .NET and .NET Core in second and third place respectively, as framework or libraries. Mostly it is the first programming language the programmers learn after the OOPs concepts. There are multiple reasons for the success of .NET, one of the most prominent ones is its stack- layered architecture. This architecture provides flexibility to add/ update layers without disturbing the whole application. .NET can be used to develop different types of high-performance applications such as gaming applications. It provides better monitoring, scalability, performance, and consistency for a whole range of applications such as console apps, GUI apps, web apps, web API apps, Windows services etc. 

.NET MVC(Model-View-Controller), provides a large set of additional functionalities to create modular applications. It eases the whole application creation process by providing component-based development and testing. 

.NET Core in nutshell
Image credit: https://codeburst.io/

In this article we will dig deeper into .NET interview questions into the following sections: 

  • Basic .NET Core interview questions 

  • Advanced .NET Core interview questions 

  • ASP.NET MVC interview questions 

 

Basic .NET Core interview questions 

1. What are the advantages of .NET Core? 

  • Cross-platform development and deployment: It can support application development on different platforms such as Windows, Linux, Mac, etc. Also, the deployment is supported on multiple platforms through containerization(Docker, Kubernetes, Service Fabric). This makes .NET completely portable and runnable on different platforms. 
  • Open-source: All .NET source code and documentation is freely available for download and contribution. This results in faster software releases, enormous support, and usage of the latest tools in development. 
  • Supports a plethora of applications: It has the capabilities to support a wide range of application types such as desktop, web, AI, cloud, mobile, IoT, gaming, etc.  
  • Secure: Provides easy-to-incorporate security measures like authentication, authorization, and data protection. It has mechanisms to protect the sensitive-data like keys, passwords, connection strings, etc. For e.g. in terms of authentication, ASP.NET Core Identity allows you to integrate your app with all major external providers.
  • High performance: With every new release of the .NET core, the performance is improved for the benefit of users. For example, in .NET 5, the garbage collection is improved for faster speed, scalability, and reduction in memory resets’ cost. Detailed account of performance improvement in .NET 5 can be found here 
  • Flexible: Provides the flexibility to use any database and infrastructure as per choice. It provides the ability to change, evolve and grow easily according to external factors. 

2. What is Kestrel? 

Kestrel architecture
Image credit: https://docs.microsoft.com/

Kestrel is an event-driven, I/O-based, open-source, cross-platform, and asynchronous server which hosts .NET applications. It is provided as a default server for .NET Core therefore, it is compatible with all the platforms and their versions which .NET Core supports. 

Usually, it is used as an edge-server, which means it is the server which faces the internet and handles HTTP web requests from clients directly. It is a listening server with a command-line interface. 

Advantages of Kestrel are: 

  • Lightweight and fast. 
  • Cross-platform and supports all versions of .NET Core. 
  • Supports HTTPS 
  • Easy configuration

 

3. What do you know about .NET Core middleware? 

Middleware is a layer, software, or simple class through which all the requests and responses have to go through. The middleware is assembled of many delegates in an application pipeline. Each component(delegate) in the pipeline of the middleware decides : 

  • To pass the request to the next component 
  • Perform some processing on the request before or after passing it. 

The below diagram shows a middleware request pipeline consisting of many delegates called one after another. Where black arrows mark the direction of execution. Each delegate in the diagram performs some operations before or after the next delegate. 

More details are here from Microsoft’s documentation. 

Delegates in Middleware pipeline
Image credit: https://docs.microsoft.com/ 

 

4. What are Razor Pages in .NET Core? 

Razor Pages is a new server-side framework which works on a page-based approach to render applications in .NET Core. They are stored as a physical .cshtml file. 

They have the HTML and code in a single file, without the need to maintain separate controllers, view models, action methods, etc. We can also have the code separate from the HTML in a different file which is attached to the Razor Page. Both types are shown below in the diagram: 

Razor Pages
Image credit: https://www.ezzylearning.net/

Razor Pages framework is flexible, lightweight, cohesive, page-based, easy to learn and maintain compared to MVC. It can be used in conjunction with traditional MVC (Model-View-Controller) architecture or Web-API controllers. 

 

5. What are service lifetimes in .NET Core? 

.NET Core supports a design pattern called ‘Dependency Injection’ which helps in the implementation of IoC(Inversion of Control). During registration, dependencies require their lifetime to be defined. The lifetime of service decides under what condition the instance of the service will be created and till what time it will be live.  

There are three types of service lifetimes supported by .NET Core: 

  • Transient Service: Instance is created each time it is requested. 
  • Scoped Service: User-specific instance is created once per user and shared across all the requests. 
  • Singleton Service: Single Instance is created once a lifetime of the application. 
 Service Lifetime in .NET Core
Image credit: https://www.c-sharpcorner.com/

 

6. What are the differences between .NET Core and .NET Framework? 

.NET Core  .NET Framework 
Completely open-source.  Few components are open-source. 
Compatible with Linux, Windows, and Mac operating systems.  Compatible with only Windows. 
Does not support desktop application development.  Supports web and desktop application development. 
Supports microservices development.  Does not support microservices development. 
Lightweight for Command Line Interface(CLI).  Heavy for Command Line Interface. 

 

Advanced .NET Core interview questions

1. What is response caching in .NET Core? 

During response caching, cache-related headers are mentioned in the HTTP responses of .NET Core MVC actions. Using these headers, we can specify how the client/proxy machine will cache responses to requests. This, in turn, reduces the number of client/proxy requests to the web server because the responses are sent from the cache itself. 

As we can see in the below diagram, the first request has a complete cycle from client browser to proxy server and then subsequently to web server. Now, the proxy server has stored the response in the cache. For all the subsequent requests, the proxy server sends the response from the cache itself. Hence, the number of proxy/client requests to the web server is reduced. 

Response Caching
Image credit: https://jakeydocs.readthedocs.io/

 
2. What is a generic host in .NET Core? 

The generic host was previously present as ‘Web Host’, in .NET Core for web applications. Later, the ‘Web Host’ was deprecated and a generic host was introduced to cater to the web, Windows, Linux, and console applications. 

Whenever a new application is started we are required to take care of the below points: 

  • Dependency Injection 
  • Configuration 
  • Logging 
  • Service lifetime management 

.NET generic host called ‘HostBuilder’ helps us to manage all the above tasks since it is built on the original abstraction of these tools. 


3. What is routing in .NET Core?
 

It is a process through which the incoming requests are mapped to the corresponding controllers and actions.  The .NET Core MVC has a routing middleware to perform this task. This middleware matches the incoming HTTP requests to the executable request-handling code. We can define the routing in the middleware pipeline in the ‘Startup.Configure’ file. 

As we can see in the below code snippet, there are two methods or pair of middleware to define routing: 

  • UseRouting : Adds route which matches the middleware pipeline.  
  • UseEndpoints : Adds end execution point to the middleware pipeline and runs the delegate of the endpoint. 
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) 
{ 
    if (env.IsDevelopment()) 
    { 
        app.UseDeveloperExceptionPage(); 
    } 

    app.UseRouting(); 

    app.UseEndpoints(endpoints => 
    { 
        endpoints.MapGet("/", async context => 
        { 
            await context.Response.WriteAsync("Hello World!"); 
        }); 
    }); 
}

 

4. What is Dependency Injection in .NET Core? Explain its advantages. 

.NET Core has been designed to support Dependency Injection(DI), which means the application is loosely coupled. It is a technique to introduce Inversion Control(IoC) between the classes and their dependencies. In other words, the object maintains only that dependency which is required during that particular task. A dependency is an object on which another object depends, by dependency injection, the application becomes better testable, maintainable, and reusable. 

Dependency Injection has three steps: 

  • An interface or base class is present to provide an abstraction for dependency implementation. 
  • Dependency is registered in a service container, a built-in container IServiceProvider is present in .NET Core. 
  • Service is injected into the constructor of the class where dependency is used. 

Advantages of Dependency Injection: 

  • Code is flexible, implementation can be changed without much overhead. 
  • Code becomes easy to test because of the use of interfaces. 
  • Code is loosely coupled, clean, and easy to maintain.

ASP.NET MVC  Interview Questions 

1. What is ASP.NET MVC? Explain its components. 

It is a lightweight and open-source web development framework, which is used to decouple data(Model), interface (View), and logic(Controller). It provides a pattern-based way to create dynamic websites and supports TDD-based development.  

mvc
Image credit: https://docs.microsoft.com/

An MVC(Model-View-Controller) architectural pattern separates the application into three components and provides separation of concerns. 

  • Model: Represents the state of application/logic, where the business logic and implementation logic is encapsulated. 
  • View: It is responsible for providing the view through the user interface. 
  • Controller: Handles user interaction, works in tandem with model and view components. 

It provides the latest web standards and many features like routing, model binding, model validation, dependency injection, web APIs, razor view engine, filters, etc.

 

2. What are the advantages of ASP.NET MVC? 

  • Provides full control over HTML rendering. 
  • Provides separation of concerns(SoC). 
  • Reduction of complexity by dividing the application into three components. 
  • Supports test-driven development(TDD). 
  • Easy integration with JavaScript, JSON, jQuery, etc.
  • Uses the latest technology and supports the latest trends. 


3. Why use an area in ASP.NET MVC?
 

Any large ASP.NET MVC project has many controllers, views, and model classes. With time, it will become very difficult to manage it using the default MVC project structure. 

 The area is used to physically partition the large application into small functional units. Each unit has its own MVC folder structure and model, view, and controller folders. 

The below example shows how each area – admin, finance, HR has its own set of model, view, and controller folders. 

Multiple areas in an MVC project
Image credit: https://www.tutorialsteacher.com/

  

4. What is the difference between ViewData and ViewBag in ASP.NET MVC? 

ViewData and ViewBag in ASP.NET MVC are used for transferring data from controller to view. Below are the differences between them: 

ViewData  ViewBag 
It is a dictionary object of the ‘ViewDataDictionary’ class having key-value.  It is a wrapper around ViewData and is a dynamic property. 
Faster than ViewBag.  Slower than ViewData. 
Type conversion code is required.  Dynamic hence type conversion code is not required. 

 

5. Describe the request flow in the ASP.NET MVC framework. 

The request flow has below stages in the MVC framework: 

  • Routing: It is the first step which matches the pattern of the request’s URL against the URL present in the route table.
  • MvcHandler: It starts the processing of the request using the ProcessRequest method.
  • Controller: Uses ‘IControllerFactory’ instance and calls the ‘Execute’ method, where ‘IControllerFactory’ is a default controller factory or a custom factory can be defined.
  • Action execution: After controller instantiation, ‘ActionInvoker’ defines which action to be performed on the controller.
  • View result: The ‘action’ method prepares the response and then returns a result.
  • View engine: ‘IViewInterface’ of the view engine selects a view engine to render the result.
  • View: ‘ViewResult’ returns and renders an HTML page on the browser. 
Request flow in ASP.NET MVC framework
Image credit: https://dotnettrickscloud.blob.core.windows.NET

 

Alt tag: Request flow in ASP.NET MVC framework 

Reference: https://dotnettrickscloud.blob.core.windows.NET/img/mvc/mvcrequestcycle.png 

 

Related Articles

Responses