NET Core Interview Question and Answers
01 What is the ASP.NET Core?
The ASP.NET Core framework is a completely open-source framework.
It will allow the ASP.NET Core apps to be deployed on various platforms or the o/s such as the macOS or Linux-based servers or certain devices.
02 What are the features provided by ASP.NET Core?
Following are the core features that are provided by the ASP.NET Core
Introduced a new, fast, and cross-platform web server - Kestrel. So, a web application can run without IIS, Apache, and Nginx.
Built-in supports for Dependency Injection
Multiple hosting
Command-line supports to creating, building, and running of the application
There is no web.config file. We can store the custom configuration in an appsettings.json file
There is no Global.asax file. We can now register and use the services in the startup class
It has good support for asynchronous programming
Support WebSocket and SignalR
Provide protection against CSRF (Cross-Site Request Forgery)
WebSocket (RFC 6455) is a protocol that enables two-way persistent communication channels over TCP connections.
It's used in apps that benefit from fast, real-time communication, such as chat, dashboard, and game apps.
What is SignalR? ASP.NET Core SignalR is an open-source library that simplifies adding real-time web functionality to apps.
Real-time web functionality enables server-side code to push content to clients instantly.
Good candidates for SignalR: Apps that require high-frequency updates from the server.
CSRF - To prevent CSRF attacks, use anti-forgery tokens with any authentication protocol where the browser silently sends credentials after the user logs in
03 What are the advantages of ASP.NET Core over ASP.NET?
It is cross-platform, so it can be run on Windows, Linux, and Mac.
There is no dependency on framework installation because all the required dependencies are shipped with our application
ASP.NET Core can handle more requests than the ASP.NET
Multiple deployment options are available withASP.NET Core
04 What are Metapackages?
The framework .NET Core 2.0 introduced Metapackage.
It helps us to do fast development as we don't require to include the individual ASP.NET Core packages.
The assembly Microsoft.AspNetCore.All is a meta package provided by ASP.NET core.
In other words, the metapackages of .NET Core describe the set of packages that are used together and acts as a parent of the child grouping structure.
The Metapackages are referenced just like any other NuGet package naming convention such as "NETStandard.Library".
05 Can ASP.NET Core application work with full .NET 4.x Framework?
Yes. ASP.NET core application works with a full .NET framework via the .NET standard library.
06 What is the startup class in ASP.NET core?
The startup class is the entry point of the ASP.NET Core application.
Every .NET Core application must have this class. This class contains the application configuration-related items.
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.
07 What is the use of the ConfigureServices method of the startup class?
This is an optional method of startup class.
It can be used to configure the services that are used by the application.
This method calls first when the application is requested for the first time.
Using this method, we can add the services to the DI container, so services are available as a dependency in the controller constructor.
08 What is the use of the Configure method of the startup class?
It defines how the application will respond to each HTTP request.
We can configure the request pipeline by configuring the middleware.
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.
09 What is middleware?
It is software that is injected into the application pipeline to handle requests and responses.
They are just like chained to each other and form as a pipeline.
The incoming requests are passed through this pipeline where all middleware is configured, and middleware can perform some action on the request before passing it to the next middleware.
10 What is the difference between IApplicationBuilder.Use() and IApplicationBuilder.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.
Use() method may call the next middleware in the pipeline added using IApplicationBuilder
The run () method never calls the subsequent middleware. After IApplicationBuilder.Run method, the system stops adding middleware in the request pipeline.
11 What is the use of the "Map" extension while adding middleware to the ASP.NET Core pipeline?
It is used for branching the pipeline.
It branches the ASP.NET Core pipeline based on request path matching.
If the request path starts with the given path, middleware on that branch will execute.
12 What is routing in ASP.NET Core?
Routing is functionality that incoming map requests to the route handler.
The route can have values (extract them from the URL) that are used to process the request.
All the routes are registered when the application is started.
The Routing uses routes to map incoming requests with the route handler and Generates the URL that is used in response.
There are two types of routing supported by ASP.NET Core
The conventional routing
Attribute routing
13 How to enable Session in ASP.NET Core?
The middleware for the session is provided by the Microsoft package. AspNetCore.Session.
To use the session in the ASP.NET Core application, we need to add this package to the csproj file and add the Session middleware to the ASP.NET Core request pipeline.
14 What are the various JSON files available in ASP.NET Core?
There are the following JSON files in ASP.NET Core :
global.json -
The global.json file allows you to define which .NET SDK version is used when you run . NET CLI commands.
launchsettings.json -
launch settings. json, which is placed in the Properties folder of a project, describes how the application can be launched
appsettings.json -
The app settings. json file is generally used to store the application configuration settings such as database connection strings, any application scope global variables, and much other information
bundleconfig.json -
The bundle config. json file now only serves as a list of input and output files for the Gulp script.
bower.json
package.json -
The package.json file is the heart of any Node project.
15 What is a tag helper in ASP.NET Core?
It is a feature provided by the Razor view engine that enables us to write server-side code to create and render the HTML element in view (Razor).
16 How to disable Tag Helper at the element level?
We can disable Tag Helper at the element level using the opt-out character ("!"). This character must apply to open and close the Html tag.
17 What are Razor Pages in ASP.NET Core?
This is a new feature introduced in ASP.NET Core 2.0. It follows a page-centric development model just like ASP.NET web forms. It supports all the features of ASP.NET Core.
Example
18 How can we do automatic model binding in Razor pages?
The Razor pages provide the option to bind property automatically when posting the data using the BindProperty attribute.
By default, it binds the properties only with non-GET verbs. we need to set the SupportsGet property to true to bind a property on getting a request.
Example
19 How can we inject the service dependency into the controller?
Step 1: Create the service
Step 2: Add this service to the Service container (service can either be added by singleton, transient, or scoped)
Step 3: Use this service as a dependency in the controller
20 How to specify the service life for a registered service that is added as a dependency?
ASP.NET Core allows us to specify the lifetime for registered services.
The service instance gets disposed of automatically based on a specified lifetime.
So, we do not care about cleaning these dependencies, it will take care of the ASP.NET Core framework. There are three types of lifetimes.
Singleton
ASP.NET Core will create and share a single instance of the service through the application life.
The service can be added as a singleton using the AddSingleton method of IServiceCollection.
ASP.NET Core creates a service instance at the time of registration and subsequent requests use this service instance.
Here, we do not require to implement the Singleton design pattern and single instance maintained by the ASP.NET Core itself.
Example
Transient
ASP.NET Core will create and share an instance of the service every time to the application when we ask for it.
The service can be added as Transient using the AddTransient method of IServiceCollection.
This lifetime can be used in stateless service. It is a way to add lightweight service.
Example
Scoped
ASP.NET Core will create and share an instance of the service per request to the application.
It means that a single instance of service is available per request. It will create a new instance in the new request.
The service can be added as scoped using an AddScoped method of IServiceCollection
We need to take care while the service is registered via Scoped in middleware and inject the service in the Invoke or InvokeAsync methods.
If we inject dependency via the constructor, it behaves like a singleton object.
No comments:
Post a Comment