In this tutorial, we are going to cover ASP.NET Core Interview Questions and Answers for beginners and experts. These interview questions will help you to prepare for the interviews from basics to advance. These interview questions will also help you to enhance your technical skills.
ASP.NET Core interview questions
Let’s start….
What is .Net Core?
.NET Core is a web framework from Microsoft. It is cross-platform. This framework supports Windows, Mac, and Linux operating systems.
These are some below specifications of the .NET Core.
- It is cross-platform.
- It is free and open-source and supported by Microsoft.
- It can be used to develop web applications.
- It is easy to use and very easy to learn.
- The latest version of .NET Core is .NET Core 5.0.
- It is also compatible with .NET Framework, Xamarin(for the mobile applications) via .NET standard library.
What is .NET 5?
.NET 5 is the latest version of .NET Core following 3.1. It provides a unified .NET SDK experience with a single base class library (BCL) across all the .NET 5 applications. It provides a single .NET platform form for all kinds of applications like Desktop application (WPF, Windows Forms, UWP), ASP.NET Core, Xamarin, and etc.
Source: Microsoft
If you want to know more about .NET 5, then click here.
What is the difference between .NET Core and .NET Framework?
Read the post about differences between .NET Core and .NET Framework.
What is Asp.Net Core?
Asp.NET Core is an open-source and cross-platform framework. It supports Windows, Mac, and Linux operating systems. Asp.NET Core applications can work with both .NET Core and .NET Framework via the .NET standard framework. It is much faster, more scalable, and modular. It is suitable for building cloud-based applications like web applications, IoT applications, and mobile applications.
What are the features in Asp.Net Core?
There are following features that are provided by the Asp.Net Core.
- It provides built-in support for Dependency Injection.
- It is cross-platform. Using Asp.Net, we can create web applications and deploy them to Windows, Linux, and Mac operating systems.
- It provides support for utilizing asynchronous programming patterns.
- It provides built-in support for the logging framework and it can be extensible.
- It provides multiple environments like development, staging, production, and etc.
- It supports multiple hosting ways. The previous version host only on IIS but Asp.Net Core will host on Apache, Docker, and self-hosting also.
- It provides the unification of development models. The MVC and Web API development models are merged and use the same base class controller.
- It provides command-line interface support to create, build and run the application.
- It provides support for WebSocket and SignalR.
- It provides protection against Cross-Site Request Forgery (CSRF).
Why use Asp.Net Core for web application development?
There are some below points that help us to understand why to use Asp.Net Core for web application development.
- Cross-Platform.
- Open-Source.
- Built-in support for Dependency Injection (DI).
- Built-in support for logging.
- Flexible deployment.
- It provides multiple environments like development, staging, production, and etc.
- Asp.Net Core is faster as compared to other Asp.Net frameworks.
- It provides support for popular JavaScript frameworks.
- It provides Razor Pages.
What is Asp.Net Core middleware and how to configure it?
Read my post about what is middleware and how to configure it?
What are the various JSON files in Asp.Net Core?
There are the following JSON files in Asp.Net Core?
- global.json
- launchsettings.json
- appsettings.json
- bundleconfig.json
- bower.json
- package.json
What is the Startup.cs file in Asp.Net Core?
Startup.cs is the entry point of the Asp.Net Core application. Startups.cs class configures the request pipeline that handles all the requests made to the application. It is not necessary that the class name mush “Startup”, it can be anything.
Startup.cs file contains two methods.
- Configure()
- ConfigureServices()
What ConfigureServices() method does in Startup.cs?
This method is optional in startup class. This method contains all the services required by the application. This method calls first when the application is requested for the first time. If you want to add services to the DI container, this method helps us to implement services to the dependency injection container.
What Configure() method does in Startup.cs?
This method will define how the application will respond to each HTTP request. This method is responsible to respond to all the HTTP requests. This method is also used to configure middlewares in the HTTP pipeline. This method accepts IApplicationBuilder as a parameter with two optional parameters (IHostingEnvironment, ILoggerFactory).
What is the difference between IApplicationBuilder.Use() vs IApplicationBuilder.Run() while adding middleware?
We can use both (IApplicationBuilder.Use(), IApplicationBuilder.Run()) methods in configure method of the startup class. Both methods help us to define inline middleware. Middlewares are executed in the same order in which they are added. When we add middleware using IApplicationBuilder.Use method, this may call the next middleware in the pipeline. On the other hand when we add middleware using IApplicationBuilder.Run method, this will never call the next or subsequent middleware in the request pipeline.
What is the use of Map extension while adding middleware in the pipeline?
Using the Map method we can implement the branching in the pipeline based on the request path matching. If the request path starts with the given path, then the middleware on to that branch will execute.
public void Configure(IApplicationBuilder app)
{
app.Map("/admin", AdminMiddleware);
app.Map("/customer", CustomerMiddleware);
}
What is routing in Asp.Net Core?
Routing is the process in which the application matches an incoming URL path and executes the corresponding action methods. In Asp.Net Core, middleware is used to match the URLs of incoming requests and map them to specific action methods. All the routes are registered when the application is started. There are two types of routing in Asp.Net Core.
- The conventional Routing
- The Attribute Routing.
How many lifetimes for registering a service as a dependency?
There are three lifetimes for register service that are added as a dependency.
- Singleton
- Transient
- Scoped
How to specify service lifetime?
Asp.Net Core allows us to specify the lifetime for registered service. Based on the lifetime, the service instance gets disposed of automatically.
Singleton:
We can create a singleton service using the AddSingleton method of IServiceCollection. Using singleton, a single instance of the service will be shared through the application life. Asp.Net Core creates a service instance at the time of registration.
services.AddSingleton<ICustomerService, CustomerService>();
Transient:
We can create the transient service using the AddTransient method of IServiceCollection. Asp.Net Core will create and share an instance of the service on every request whenever we ask for it. This lifetime is best for stateless and lightweight services
services.AddTransient<ICustomerService, CustomerService>();
Scoped:
We can create the scoped service using the AddScoped method of IServiceCollection. Asp.Net Core will create and share an instance of the service per request to the application. It means, whenever we request then a new instance will be created.
services.AddScoped<ICustomerService, CustomerService>();
What is the wwwroot folder in Asp.Net Core?
All the static files are now located in the wwwroot folder by default. All the CSS, Images, JS, and other static content are located in the wwwroot folder. We can also change the name of this folder.
What is tag helper in Asp.Net Core?
Tag helper is the feature that is provided by the Razor View engine. Tag Helpers enable us to write server-side code to create and render the HTML element in the Razor view file. It is very similar to the HTML helper of Asp.Net MVC. There are many built-in Tag helpers like Image, From, Anchor, and etc.
//content with tag helper
<input asp-for="CustomerName" placeholder="Enter Your Customer Name" class="form-control"/>
//Equivalent HTML
<input placeholder="Enter Your Customer Name" class="form-control" id="CustomerName" name="CustomerName" value="" type="text">
How to disable tag helper at element level?
We can disable the tag helper at the element level using the (“!”) character. This character must apply to the opening and the closing HTML tag.
<!span asp-validation-for=”customer” class=”customer”></!span>
How to enable session in Asp.Net Core?
We can enable the session by adding the session middleware in the Asp.Net Core request pipeline as you do see below in the startup class.
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
//other services
services.AddSession();
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseSession();
.....
}
}
Can Asp.Net Core applications work with the full .NET 4.x framework?
Yes, Asp.Net Core applications can work with the full .NET framework using the .NET standard library.
What is a .csproj file in Asp.Net Core?
This file is used to manage all the NuGet packages for our application.
What is Asp.Net Core Module?
Asp.Net Core Module lets you run Asp.Net Core apps behind IIS. It works only with a kestrel. It is not compatible with Weblistener. Asp.Net Core is a native IIS module that hooks into the IIS pipeline and redirects traffic to the backend ASP.NET Core application. Asp.Net Core applications run in a process separate from the IIS worker process. It also does process management. Asp.Net Core module starts the process for the Asp.Net Core application when the first request comes in and restarts it when it crashes.
What is Dependency Injection in Asp.net Core?
Dependency Injection (DI) is basically a design pattern of application. It is a coding pattern in which a class adds functionality from external sources rather than creating them itself.
How can we inject the service dependency into the controller?
There are the following steps to add custom service as a dependency.
Step 1: Create the service
public interface ICustomerService
{
string GetCustomer();
}
public class CustomerService: ICustomerService
{
public string GetCustomer()
{
return "Hi!";
}
}
Step 2: Add this service to the Service container (service can either added by singleton, transient, or scoped)
public void ConfigureServices(IServiceCollection services)
{
//other services
services.AddTransient<ICustomerService, CustomerService>();
//other services
}
Step 3: Use this service as a dependency in the controller
public class HomeController: Controller
{
ICustomerService _customerService;
public HomeController(ICustomerService customerService)
{
_customerService = customerService;
}
}
What is Metapackage?
The metapackage was introduced by .NET Core 2.0 framework. It includes all the supported packages by Asp.Net Core with their dependencies into one package. After including this package, we don’t need to include the individual Asp.Net Core packages. It helps us to do fast development. The assembly “Microsoft.AspNetCore.All” is a metapackage provided by Asp.Net Core.
What is Microservices in Asp.Net Core?
Read My Post About Microservices:
- SOA (Service-Oriented Architecture) Vs Microservices Architecture
- Microservices Architecture with Ocelot API gateway in Asp.Net Core
What are the bundling and minification, and what is the built-in tool for Asp.Net Core?
Bundling: It is the process of combining multiple files into a single file. So, we can create Javascript, CSS, and other bundles.
Minification: It is the process of removing unnecessary data without affecting functionality. In minification, it removes extra spaces, comments, and etc.
Built-In tool: BundlerMinifier is the built-in tool and available as an extension.
What are the different ways for bundling and minification in Asp.Net Core?
There are different ways for bundling and minification in Asp.Net Core.
- Asp.Net Core Web Optimizer: it is the middleware for bundling and minification of CSS and Javascript files at runtime.
- Grunt.
- BundlerMinifier: It is the visual studio extension.
What is the IHostingEnvironment in Asp.Net Core?
Asp.Net Core provides an interface with the name of IHostingEnvironment. It allows us to retrieve the current environment. By default, there are three different environments like Development, Staging, and Production. We can also define our own custom environment.
What is Kestrel?
Kestrel is an open-source, cross-platform, light-weight, and default web server used for the Asp.Net Core application. Asp.Net Core applications run the kestrel web server as an in-process server to handle the web request. It is based on libuv, which is a cross-platform asynchronous I/O library.
By default, When we create a new Asp.Net Core project, the kestrel web server includes the Asp.Net Core project template. Kestrel provides better request processing performance to Asp.Net Core projects because it has lightweight nature. But, it doesn’t have any advanced features like IIS, Apache, and Nginx.
Kestrel have some features:
- Kestrel web server supports SSL.
- It is cross platform, runs in Linux, Mac, and Windows operating systems.
What is WebListener?
WebListener is one of the web servers of Asp.Net Core. It runs only on windows. It’s built on HTTP.Sys kernel-mode driver. It is an alternative to Kestrel.
What does WebHost.CreateDefaultBuilder() do?
There are following things that WebHost.CreateDefaultBuilder() do:
- It adds routing.
- It adds IIS Integration.
- It tells the app to use kestrel as a web server.
- It configures command line arguments.
- It specifies to use the current project directory as the root directory for the application.
- It configures the default service provider.
- It adds console/debug loggers.
- If the application is in the development environment, then allows the loading of secrets.
- It configures environment variables to the configuration object.
- It configures both (appsettings.json and appsettings.{Env}.json) to be loaded into the configuration object.
What is Host in Asp.Net Core application?
Asp.Net Core applications require a host in which to execute. It is responsible for the application startup and lifetime management. It configures the application’s server and services.
What is the difference between Host and Server?
Host: It is responsible for starting the Asp.Net Core application and its lifetime management. It is configured to use a particular server.
Server: It is responsible for accepting the HTTP requests. It is unaware of its host.
Leave a Reply