Today we will discuss, the most asked and hot ASP.NET Core interview questions and answers, that you should know before any ASP.NET Core interview.
Let’s gets started,
1. What is ASP.NET Core?
ASP. NET Core is a new version Of ASP.NET and it is open source. It is not an upgraded version Of ASP.NET it’s completely rewritten. It is cross-platform, supporting Windows, macOS, and Linux, and can be used in devices, clouds, and scenarios.
It can work with both .NET Core and .net framework via the .NET standard framework. It is best suitable for developing cloud-based such as web applications, mobile applications, IoT applications. Command-line supports to create, build and run the application.
2. What are the features/characteristics provided by ASP.NET core?
- Single programming module for MVC and Web API.
- Support Web Socket and SignalR.
- Command Line Support to Create, build and run applications.
- Built-in support for logging framework and it can be extensible.
- It has good support for asynchronous programming.
- There is no web.config file. We can store the custom configuration into an appsettings.json file
- There is no Global.asax file. We can now register and use the services in startup class.
- Multiple hosting ways are supported.
- Provide protection against CSRF (Cross-Site Request Forgery).
- Built-In support of dependencies injection.
3. What is dependency injection?
- Dependency injection (DI) is a software design pattern.
- It is a technique for achieving Inversion of control (IoC) between classes and their dependencies.
- It allows us to develop loosely-coupled code.
- It makes code maintainable.
- It also helps to reduce the tight coupling between software components.
- DI enables us to better manage future changes and other complexity in our software.
4. What is the role of Startup class?
- The startup class is the entry point of the ASP.NET Core application.
- It is not necessary that the class name must be “Startup”, it can be anything, we can configure the startup class in the program class.
- It is responsible for configuration-related things as below.
- It configures the services which are required by the app.
- It defines the app’s request handling pipeline as a series of middleware components.
- The startup class is specified inside ‘CreateHostBuilder’ when the host is created.
- Multiple Startup classes can also be defined for different environments, At run time appropriate startup class is used.
- ConfigureServices method is optional and defined inside the startup class.
5. What is the role of ConfigureServices and Configure method?
- It takes care of registering services that are consumed across the application using Dependency Injection (D1) or Application Services.
- It’s get called by host before ‘Configure’ method to configure the app’s services.
- Configure method is used to add middleware components to IApplicationBuilder instance that’s
- available in Configure method.
- It accepts IApplicationBuilder as a parameter and also it has two optional parameters:
- IHostingEnvironment and ILoggerFactory.
- Using this method, we can configure built-in middleware such as routing, authentication, session, etc. as well as third-party middleware.
- Configure method also specify how the app respond to HTTP request and response.
6. What is wwwroot folder in ASP.NET Core?
- The files can be stored here and with a relative path to the root.
- Only these files inside the wwwroot can be served over HTTP Requests. All other files are filtered out and cannot be served by default.
7. What is middleware?
- A middleware is nothing but a component (class) that is executed on every request in ASP. NET Core application.
- It is software that is injected into the application pipeline to handle requests and responses.
- The middleware component is a program that’s built into a pipeline to handle the request and response.
- Each middleware component can decide whether to pass the request to next component and to perform any operation before or after the next component in the pipeline.
- It is used to implement several tasks when handling requests.
8. What’s the difference between .NET Core Framework and .NET Standard?
- .Net Standard is a specification for implementing the Base Class Library (BCL). BCL contains classes such as exception handling, XML, collections, 1/0, and networking. WPF, WCF, and ASP.NET do not form part Of BCL and so are not included in the .NET Standard library.
- .NET Core is a managed framework that builds desktop and web applications cross-platform.
- Both “.NET Core” and “.NET Framework” include .NET Standard for BCL in their managed framework.
- .NET Framework is a framework that builds desktop and web applications in Windows only. It is highly dependent on the architecture.
9. What is Kestrel?
- Kestrel is a cross-platform web server for ASP.NET Core based on Iibuv, a cross-platform asynchronous I/0 library.
- Kestrel is the webserver that is included by default in ASP.NET Core new project templates.
- It is fast and secure and can even be used without a reverse proxy server. However, it is still
- recommended to use with 11S, Nginx, or Apache.
- A reverse proxy server receives HTTP requests from the Internet and forwards them to Kestrel after some preliminary handling.
- Kestrel is relatively new and does not yet have a fun complement of defenses against attacks.
10. What is the difference between IApplicationBuilder.Use() and IApplicationBuiIder.Run()?
- We can use both the methods in Configure methods of the startup class
- Both are used to add middleware delegates to the application request pipeline.
- The middleware adds using IApplicationBuilder.lJse may call the next middleware in the pipeline whereas the middleware adds using [ApplicationBuilder.Run method never calls the subsequent or next middleware.
- After IApplicationBuilder.Run method, system stop adding middleware in the request pipeline.
11. What is the difference between services.AddTransient & service.AddScoped & service.AddSingIeton methods are Asp-Net Core?
Transient objects are created for every request (when requested).
The service can be added as Transient using the AddTransient method of IServiceCollection.
This lifetime can be used in stateless service and it is a way to add lightweight service.
Scoped objects are the same within a request, but different across different requests.
ASP.NET Core will create and share an instance of the service per request to the application.
It will create a new instance in the new request.
The service can be added as scoped using an AddScoped method of IServiceCollection.
Singleton objects are created the first time they’re requested (or when ConfigureServices is run and an instance is specified with the service registration).
The service can be added as a singleton using the AddSingleton method of IServiceCollection.
ASP.NET Core creates service instance at the time of registration and subsequence request use this service instance.
12. What is WebListener?
- WebListener is a web server in ASP.NET Core that is only on Windows host machines.
- It is an alternative to Kestrel and is built on HttpSys kernel-mode driver.
- Also, is used for direct connection to the internet without the need of an IIS as a reverse proxy server.
- It is not compatible with IIS.
13. What are the various JSON files available in ASP.NET Core?
Following JSON files are available in the ASP.NET Core
14. Explain routing in ASP.NET Core
- Routing is functionality that maps incoming requests to the route handler.
- The Routing uses routes to map incoming requests with the route handler and Generates URL that is used in response.
- The route can have values (extract them from URL) that are used to process the request.
- Using the route, routing can find a route handler based on the URL.
- All the routes are when the application is started.
There are two types Of touting supported by ASP.NET Core.
- The conventional routing
- Attribute routing
15. How to enable Session in ASP.NET Core?
- The middleware for the session is provided by the package Microsoft.AspNetCore.Session.
- To use the session in ASP. NET Core application, we need to add this package to csproj file and add the session middleware to the ASP.NET Core request pipeline.
Thank you for reading. Visit Mycodebit.com for .net core and .net tutorial.