Friday, May 5, 2023

How to enable Session in ASP.NET Core?

The middleware for the session is provided by the package Microsoft.AspNetCore.Session.

1. To use the session in ASP.NET Core application, we need to add this package to csproj file and add the Session middleware to ASP.NET Core request pipeline.

public void Configure(IApplicationBuilder app, TwebHostEnvironment env)

{

if(env.IsDevelopment())

app.UseDeveloperExceptionPage();

else

{

app.UseExceptionHandler("/Home/Error");

//The default HSTS value is 30 days. You may want to change this for production

app.UseŠ½sts();

}

app.UseSession();

app.UseHttpsRedirection(); app.UseStaticFiles();

app.UseAuthentication();

app.UseRouting();

app.UseAuthorization();

app.useEndpoints (endpoints =>

{

endpoints.MapController Route(

name: "default",

pattern: "(controller-Home)/(action-Index}/{id}");

});

}

Explain routing in ASP.NET Core

► Routing is functionality that map incoming request to the route handler.

► The Routing uses routes for map incoming request with route handler and Generate URL that used in response.

►The route can have values (extract them from URL) that used to process the request.

►Using the route, routing can find route handler based on URL.

► All the routes are registered when the application is started.

► There are two types of touting supported by ASP.NET Core.

1.The conventional routing

2.Attribute routing

What are the various JSON files available in ASP.NET Core?

Following JSON files are available in the ASP.NET Core

appsettings.json

bundleconfig.json

launchsettings.json

bower.json

package.json

global.json

What is WebListener?

► WebListener is a web server in ASP.NET Core that runs 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.

What is the difference between services.AddTransient & service.AddScoped & service.AddSingleton methods are Asp.Net Core?

 Transient objects are created for every request (when requested).

 The service can be added as Transient using 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.

he service can be added as scoped using an AddScoped method of IServiceCollection.

Singleton objects created the first time they're requested (or when ConfigureServices is run and an instance is specified with the service registration).


What is the difference between IApplicationBuilder.Use() and IApplicationBuilder.Run()?

► We can use both the methods in Configure methods of startup class.

Both are used to add middleware delegate to the application request pipeline.

►The middleware adds using IApplication Builder. Use may call the next middleware in the pipeline whereas the middleware adds using IApplication Builder.Run method never calls the subsequent or next middleware.

► After IApplicationBuilder.Run method, system stop adding middleware in request pipeline.

What is Kestrel?

► Kestrel is a cross-platform web server for ASP.NET Core based on libuv, a cross-platform asynchronous I/O library.

► Kestrel is the web server 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 IIS, 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 full complement of defenses against attacks.


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, I/O and networking. WPF, WCF and ASP.NET do not form part of BCL and so are not included in .NET Standard library.

► .NET Core is a managed framework that builds desktop and web applications in 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.

What is middleware?

► A middleware is nothing but a component (class) which is executed on every request in ASP.NET Core application.

► It is software which is injected into the application pipeline to handle request and responses.

► Middleware component is program that's build into an app's 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 next component in pipeline.

What is wwwroot folder in ASP.NET Core?

► By default the wwwroot is the root folder that contains the static files such as HTML, CSS and Javascript.

►The files can be stored here and accessed 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.

What is the role of ConfigureServices and Configure method?

► ConfigureServices method is optional and defined inside startup class.

► It takes care of registering services that are consumed across the application using Dependency Injection (DI) 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 IApplication Builder instance that's available in Configure method.

► It accepts IApplication Builder 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.

Where is startup class in ASP.NET Core?

ASP.NET Core apps use a Startup class, which is named Startup by convention. The Startup class: Optionally includes a ConfigureServices method to configure the app's services. A service is a reusable component that provides app functionality.

What is dependency injection in MVC?

The Dependency Injection pattern is a particular implementation of Inversion of Control. Inversion of Control (IoC) means that objects do not create other objects on which they rely to do their work. Instead, they get the objects that they need from an outside source (for example, an xml configuration file).

Dependency Injection (DI) means that this is done without the object intervention, usually by a framework component that passes constructor parameters and set properties.