{"id":1793,"date":"2023-06-14T11:34:55","date_gmt":"2023-06-14T11:34:55","guid":{"rendered":"http:\/\/waqar-arshad.com\/?p=1793"},"modified":"2023-08-01T07:19:38","modified_gmt":"2023-08-01T07:19:38","slug":"top-60-asp-net-core-interview-questions","status":"publish","type":"post","link":"http:\/\/waqar-arshad.com\/index.php\/2023\/06\/14\/top-60-asp-net-core-interview-questions\/","title":{"rendered":"Top 60 ASP.NET Core Interview Questions"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"1793\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">    <\/span>\r\n<\/div><div class=\"pld-dislike-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-dislike-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"1793\" data-trigger-type=\"dislike\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-down\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-dislike-count-wrap pld-count-wrap\"><\/span>\r\n<\/div><\/div>\n<p><strong>1. Describe the ASP.NET Core.<\/strong><\/p>\n\n\n\n<p><strong>ASP.NET Core<\/strong>&nbsp;is an open-source, cross-platform and high performance platform that allows you to build modern, Internet-connected and cloud enabled applications. With ASP.NET Core you can<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>build web applications, IoT (Internet of things) apps, services and mobile Backends.<\/li>\n\n\n\n<li>run on .Net Core.<\/li>\n\n\n\n<li>You can do your development on Linux, Windows and MacOS.<\/li>\n\n\n\n<li>deploy your code to cloud or on-premises.<\/li>\n<\/ul>\n\n\n\n<p><strong>2. What are the benefits of using ASP.NET Core over ASP.NET?<\/strong><\/p>\n\n\n\n<p>ASP.NET Core comes with the following benefits over&nbsp;<strong>ASP.NET<\/strong>.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Cross platform, provide ability to develop and run on Windows, Linux and MacOS.<\/li>\n\n\n\n<li>Open-source<\/li>\n\n\n\n<li>Unified Platform to develop Web UI and services.<\/li>\n\n\n\n<li>Built-in dependency injection.<\/li>\n\n\n\n<li>Ability to deploy on more than one server like IIS, Kestrel, Nginx, Docker, Apache etc<\/li>\n\n\n\n<li>cloud enabled framework, provide support for environment based configuration systems.<\/li>\n\n\n\n<li>Lightweight, High performance and modern HTTP request pipelines.<\/li>\n\n\n\n<li>well suited architecture for testability<\/li>\n\n\n\n<li>Integration of many client-side frameworks like&nbsp;<a href=\"https:\/\/www.qfles.com\/interview-question\/angular-interview-questions\">Angular any version<\/a><\/li>\n\n\n\n<li>Blazor allow you to use&nbsp;<a href=\"https:\/\/www.qfles.com\/interview-question\/c-sharp-interview-questions\">C# code<\/a>&nbsp;in browser with&nbsp;<a href=\"https:\/\/www.qfles.com\/interview-question\/java-script-interview-questions\">JavaScript code<\/a>.<\/li>\n<\/ul>\n\n\n\n<p><strong>3. What is the role of Startup class?<\/strong><\/p>\n\n\n\n<p><strong>Startup class<\/strong>&nbsp;is responsible for configuration related things as below.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It configures the services which are required by the app.<\/li>\n\n\n\n<li>It defines the app&#8217;s request handling pipeline as a series of middleware components.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Startup class example<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Startup <\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; public Startup(IConfiguration configuration)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; {<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Configuration = configuration;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; public IConfiguration Configuration { get; }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; public void ConfigureServices(IServiceCollection services)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; {<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; services.AddRazorPages();<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; public void Configure(IApplicationBuilder app, IWebHostEnvironment env)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; {<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (env.IsDevelopment())<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app.UseDeveloperExceptionPage();<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app.UseExceptionHandler(\"\/Error\");<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app.UseHsts();<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app.UseHttpsRedirection();<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/ other middleware components<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>}<\/code><\/pre>\n\n\n\n<p>Startup class is specified inside the &#8216;CreateHostBuilder&#8217; method when the host is created.<br>Multiple Startup classes can also be defined for different environments, At run time appropriate startup classes are used.<\/p>\n\n\n\n<p><strong>4. What is the role of ConfigureServices and Configure method?<\/strong><\/p>\n\n\n\n<p><strong>ConfigureServices<\/strong>&nbsp;method is optional and defined inside startup class as mentioned in above code. It gets called by the host before the &#8216;Configure&#8217; method to configure the app&#8217;s services.<br><strong>Configure<\/strong>&nbsp;method is used to add middleware components to the IApplicationBuilder instance that&#8217;s available in Configure method. Configure method also specifies how the app responds to HTTP request and response. ApplicationBuilder instance&#8217;s &#8216;Use&#8230;&#8217; extension method is used to add one or more middleware components to request pipeline.<br>You can configure the services and middleware components without the Startup class and it&#8217;s methods, by defining this configuration inside the Program class in&nbsp;<strong>CreateHostBuilder<\/strong>&nbsp;method.<\/p>\n\n\n\n<p><strong>5. Describe the Dependency Injection.<\/strong><\/p>\n\n\n\n<p><strong>Dependency Injection<\/strong>&nbsp;is a&nbsp;<a href=\"https:\/\/www.qfles.com\/interview-question\/csharp-design-patterns-interview-questions\">Design Pattern<\/a>&nbsp;that&#8217;s used as a technique to achieve the&nbsp;<a href=\"https:\/\/www.qfles.com\/interview-question\/solid-principles-interview-questions\">Inversion of Control (IoC)<\/a>&nbsp;between the classes and their dependencies.<br>ASP.NET Core comes with a built-in Dependency Injection framework that makes configured services available throughout the application. You can configure the services inside the ConfigureServices method as below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>services.AddScoped();<\/code><\/pre>\n\n\n\n<p>A Service can be resolved using constructor injection and DI framework is responsible for the instance of this service at run time. For more visit&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/aspnet\/core\/fundamentals\/dependency-injection?view=aspnetcore-5.0\" target=\"_blank\" rel=\"noreferrer noopener\">ASP.NET Core Dependency Injection<\/a><\/p>\n\n\n\n<p><strong>6. Explain the request processing pipeline in ASP.NET Core.<\/strong><\/p>\n\n\n\n<p>In order to understand the Request Processing Pipeline in ASP.NET Core, concept, let us modify the Configure() method of the Startup class as shown below. Here we are registering three middleware components into the request processing pipeline. As you can see the first two components are registered using the&nbsp;<strong>Use()<\/strong>&nbsp;extension method so that they have the chance to call the next middleware component in the request processing pipeline. The last one is registered using the&nbsp;<strong>Run()<\/strong>&nbsp;extension method as it is going to be our terminating components i.e. it will not call the next component.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"581\" height=\"386\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/123.webp\" alt=\"\" class=\"wp-image-1794\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/123.webp 581w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/123-300x199.webp 300w\" sizes=\"auto, (max-width: 581px) 100vw, 581px\" \/><\/figure>\n\n\n\n<p><strong>7. Explain the difference between\u00a0<code>app.Run<\/code>\u00a0and\u00a0<code>app.Use<\/code>\u00a0in ASP.NET Core.<\/strong><\/p>\n\n\n\n<p>mentioned above<\/p>\n\n\n\n<p><strong>8. What problems does Dependency Injection solve?<\/strong><\/p>\n\n\n\n<p>Let&#8217;s understand Dependency Injection with this C# example. A class can use a direct dependency instance as below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Public class A {<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>MyDependency dep = new MyDependency();<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>public void Test(){<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>dep.SomeMethod();<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>}<\/code><\/pre>\n\n\n\n<p>But these direct dependencies can be problematic for the following reasons.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If you want to replace &#8216;MyDependency&#8217; with a different implementation then the class must be modified.<\/li>\n\n\n\n<li>It&#8217;s difficult to Unit Test.<\/li>\n\n\n\n<li>If MyDependency class has dependencies then it must be configured by class. If Multiple classes have dependency on &#8216;MyDependency&#8217;, the code becomes scattered.<\/li>\n<\/ul>\n\n\n\n<p><strong>DI framework<\/strong>&nbsp;solves these problems as below.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use Interfaces or base class to abstract the dependency implementation.<\/li>\n\n\n\n<li>Dependencies are registered in the Service Container provided by ASP.NET Core inside Startup class &#8216;ConfigureServices&#8217; method.<\/li>\n\n\n\n<li>Dependencies are injected using constructor injection and the instance is created by DI and destroyed when no longer needed.<\/li>\n<\/ul>\n\n\n\n<p><strong>9. Describe the Service Lifetimes.<\/strong><\/p>\n\n\n\n<p>When Services are registered, there is a lifetime for every service. ASP.NET Core provides the following lifetimes.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Transient<\/strong>&nbsp;&#8211; Services with transient lifetime are created each time they are requested from service container. So it&#8217;s best suited for stateless, light weight services.<\/li>\n\n\n\n<li><strong>Scoped<\/strong>&nbsp;&#8211; Services with scoped lifetime are created once per connection or client request. When using scoped service in middleware then inject the service via invoke or invokeAsync method. You should not inject the service via constructor injection as it treats the service behavior like Singleton.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.qfles.com\/interview-question\/csharp-design-patterns-interview-questions\">Singleton<\/a>&nbsp;&#8211; Service with singleton lifetime is created once when first time the service is requested. For subsequent requests same instance is served by service container.<\/li>\n<\/ul>\n\n\n\n<p><strong>10. Explain the Middleware in ASP.NET Core.<\/strong><\/p>\n\n\n\n<p>The Request handling pipeline is a sequence of middleware components where each component performs the operation on request and either call the next middleware component or terminate the request. When a middleware component terminates the request, it&#8217;s called&nbsp;<strong>Terminal Middleware<\/strong>&nbsp;as It prevents next middleware from processing the request. You can add a middleware component to the pipeline by calling&nbsp;<strong>.Use&#8230;<\/strong>&nbsp;extension method as below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; app.UseHttpsRedirection();<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; app.UseStaticFiles();<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; app.UseRouting();<\/code><\/pre>\n\n\n\n<p>So&nbsp;<strong>Middleware component<\/strong>&nbsp;is program that&#8217;s build into an app&#8217;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.<\/p>\n\n\n\n<p><strong>11. What is Request delegate?<\/strong><\/p>\n\n\n\n<p><strong>Request delegates<\/strong>&nbsp;handle each HTTP request and are used to build request pipeline. It can configured using Run, Map and Use extension methods. An request delegate can be a in-line as an anonymous method (called in-line middleware) or a reusable class. These classes or in-line methods are called middleware components.<\/p>\n\n\n\n<p><strong>12. What is Host in ASP.NET Core?<\/strong><\/p>\n\n\n\n<p><strong>Host<\/strong>&nbsp;encapsulates all the resources for the app. On startup, ASP.NET Core application creates the host. The Resources which are encapsulated by the host include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>HTTP Server implementation<\/li>\n\n\n\n<li>Dependency Injection<\/li>\n\n\n\n<li>Configuration<\/li>\n\n\n\n<li>Logging<\/li>\n\n\n\n<li>Middleware components<\/li>\n<\/ul>\n\n\n\n<p><strong>13. Describe the Generic Host and Web Host.<\/strong><\/p>\n\n\n\n<p>The host setup the server, request pipeline and responsible for app startup and lifetime management. There are two hosts:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>.NET Generic Host<\/li>\n\n\n\n<li>ASP.NET Core Web Host<\/li>\n<\/ul>\n\n\n\n<p><strong>.NET Generic Host<\/strong>&nbsp;is recommended and ASP.NET Core template builds a .NET Generic Host on app startup.<br><strong>ASP.NET Core Web host<\/strong>&nbsp;is only used for backwards compatibility.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Host creation<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Program<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; public static void Main(string&#91;] args)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; {<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CreateWebHostBuilder(args).Build().Run();<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; public static IWebHostBuilder CreateWebHostBuilder(string&#91;] args) =&gt;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WebHost.CreateDefaultBuilder(args)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .UseStartup();<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>}<\/code><\/pre>\n\n\n\n<p><strong>14. Describe the Servers in ASP.NET Core.<\/strong><\/p>\n\n\n\n<p>Server is required to run any application.&nbsp;<strong>ASP.NET Core<\/strong>&nbsp;provides an in-process HTTP server implementation to run the app. This server implementation listen for HTTP requests and surface them to the application as a set of request features composed into an HttpContext.<br>ASP.NET Core use the&nbsp;<strong>Kestrel<\/strong>&nbsp;web server by default. ASP.NET Core comes with:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Default Kestrel web server that&#8217;s cross platform HTTP server implementation.<\/li>\n\n\n\n<li>IIS HTTP Server that&#8217;s in-process server for IIS.<\/li>\n\n\n\n<li>HTTP.sys server that&#8217;s a Windows-only HTTP server and it&#8217;s based on the HTTP.sys kernel driver and HTTP Server API.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>15. How Configuration works in ASP.NET Core?<\/strong><\/p>\n\n\n\n<p>In ASP.NET Core,&nbsp;<strong>Configuration<\/strong>&nbsp;is implemented using various configuration providers. Configuration data is present in the form of key value pairs that can be read by configuration providers as key value from different configuration sources as below.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>appsettings.json &#8211; settings file<\/li>\n\n\n\n<li>Azure Key Vault<\/li>\n\n\n\n<li>Environment variables<\/li>\n\n\n\n<li>In-memory .Net objects<\/li>\n\n\n\n<li>Command Line Arguments<\/li>\n\n\n\n<li>Custom Providers<\/li>\n<\/ul>\n\n\n\n<p>By default apps are configured to read the configuration data from appsettings.json, environment variables, command line arguments etc. While reading the data, values from environment variables override appsettings.json data values. &#8216;CreateDefaultBuilder&#8217; method provide default configuration.<\/p>\n\n\n\n<p><strong>16. How to read values from Appsettings.json file?<\/strong><\/p>\n\n\n\n<p>You can read values from appsettings.json using below code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>class Test{<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ requires using Microsoft.Extensions.Configuration;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code> private readonly IConfiguration Configuration;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; public TestModel(IConfiguration configuration)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; {<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Configuration = configuration;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ public void ReadValues(){<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>var val = Configuration&#91;\"key\"]; \/\/ reading direct key values<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>var name = Configuration&#91;\"Employee:Name\"]; \/\/ read complex values<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>}<\/code><\/pre>\n\n\n\n<p>Default configuration provider first load the values from appsettings.json and then from appsettings.Environment.json file.<br>Environment specific values override the values from appsettings.json file. In development environment appsettings.Development.json file values override the appsettings.json file values, same apply to production environment.<br>You can also read the appsettings.json values using options pattern described&nbsp;<a href=\"https:\/\/www.qfles.com\/blogs\/technology\/read-values-from-appsettings-json-file\">Read values from appsettings.json file<\/a>.<\/p>\n\n\n\n<p><strong>17. What is the Options Pattern in ASP.NET Core?<\/strong><\/p>\n\n\n\n<p><strong>Options Pattern<\/strong>&nbsp;allow you to access related configuration settings in Strongly typed way using some classes. When you are accessing the configuration settings with the isolated classes, The app should adhere these two principles.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Interface Segregation Principle (ISP) or Encapsulation:<\/strong>&nbsp;The class the depend on the configurations, should depend only on the configuration settings that they use.<\/li>\n\n\n\n<li><strong>Separation of Concerns:<\/strong>&nbsp;Settings for different classes should not be related or dependent on one another.<\/li>\n<\/ul>\n\n\n\n<p><strong>18. How to use multiple environments in ASP.NET Core?<\/strong><\/p>\n\n\n\n<p>ASP.NET Core use environment variables to configure application behavior based on runtime environment. launchSettings.json file sets&nbsp;<code>ASPNETCORE_ENVIRONMENT<\/code>&nbsp;to&nbsp;<code>Development<\/code>&nbsp;on local Machine. For more visit&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/aspnet\/core\/fundamentals\/environments?view=aspnetcore-5.0\" target=\"_blank\" rel=\"noreferrer noopener\">How to use multiple environments in ASP.NET Core<\/a><\/p>\n\n\n\n<p><strong>19. How Logging works in .NET Core and ASP.NET Core?<\/strong><\/p>\n\n\n\n<p>In ASP.NET Core,&nbsp;<strong>logging providers store the logs<\/strong>. You can configure multiple logging providers for your application. The default ASP.NET Core configures the following logging providers: Console, Debug, EventSource, and EventLog<\/p>\n\n\n\n<p><strong>20. How Routing works in ASP.NET Core?<\/strong><\/p>\n\n\n\n<p><strong>Routing<\/strong>&nbsp;is used to handle incoming HTTP requests for the app. Routing find matching executable endpoint for incoming requests. These endpoints are registered when app starts. Matching process use values from incoming request url to process the requests. You can configure the routing in middleware pipeline of configure method in startup class.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; app.UseRouting(); \/\/ It adds route matching to middlware pipeline<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; \/\/ It adds endpoints execution to middleware pipeline<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; app.UseEndpoints(endpoints =&gt;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; {<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; endpoints.MapGet(\"\/\", async context =&gt;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; {<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; await context.Response.WriteAsync(\"Hello World!\");<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; });<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; });<\/code><\/pre>\n\n\n\n<p>For more you can refer&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/aspnet\/core\/fundamentals\/routing?view=aspnetcore-5.0\" target=\"_blank\" rel=\"noreferrer noopener\">ASP.NET Core Routing<\/a><\/p>\n\n\n\n<p><strong>21. How to handle errors in ASP.NET Core?<\/strong><\/p>\n\n\n\n<p>ASP.NET Core provides a better way to handle the errors in Startup class as below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; if (env.IsDevelopment())<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; {<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; app.UseDeveloperExceptionPage();<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; else<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; {<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; app.UseExceptionHandler(\"\/Error\");<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; app.UseHsts();<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; }<\/code><\/pre>\n\n\n\n<p>For development environment, Developer exception page display detailed information about the exception. You should place this middleware before other middlewares for which you want to catch exceptions. For other environments&nbsp;<code>UseExceptionHandler<\/code>&nbsp;middleware loads the proper Error page.<br>You can configure error code specific pages in Startup class&nbsp;<code>Configure<\/code>&nbsp;method as below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; app.Use(async (context, next) =&gt;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; {<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; await next();<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; if (context.Response.StatusCode == 404)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; {<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; context.Request.Path = \"\/not-found\";<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; await next();<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; if (context.Response.StatusCode == 403 || context.Response.StatusCode == 503 || context.Response.StatusCode == 500)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; {<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; context.Request.Path = \"\/Home\/Error\";<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; await next();<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; });<\/code><\/pre>\n\n\n\n<p>For more visit&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/aspnet\/core\/fundamentals\/error-handling?view=aspnetcore-3.1\" target=\"_blank\" rel=\"noreferrer noopener\">Error handling<\/a><\/p>\n\n\n\n<p><strong>22. How ASP.NET Core serve static files?<\/strong><\/p>\n\n\n\n<p>In ASP.NET Core,&nbsp;<strong>Static files<\/strong>&nbsp;such as CSS, images, JavaScript files, HTML are the served directly to the clients. ASP.NET Core template provides a root folder called&nbsp;<code>wwwroot<\/code>&nbsp;which contains all these static files.&nbsp;<code>UseStaticFiles()<\/code>&nbsp;method inside&nbsp;<code>Startup.Configure<\/code>&nbsp;enables the static files to be served to client.<br>You can serve files outside of this webroot folder by configuring Static File Middleware as following.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>app.UseStaticFiles(new StaticFileOptions<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; {<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FileProvider = new PhysicalFileProvider(<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Path.Combine(env.ContentRootPath, \"MyStaticFiles\")), \/\/ MyStaticFiles is new folder<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;RequestPath = \"\/StaticFiles\"&nbsp; \/\/ this is requested path by client<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; });<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ now you can use your file as below<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;img src=\"\/StaticFiles\/images\/profile.jpg\" class=\"img\" alt=\"A red rose\" \/&gt;<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code> \/\/ profile.jpg is image inside MyStaticFiles\/images folder<\/code><\/pre>\n\n\n\n<p><strong>23. Explain Session and State management in ASP.NET Core<\/strong><\/p>\n\n\n\n<p>As we know HTTP is a stateless protocol. HTTP requests are independent and does not retain user values. There are different ways to maintain user state between multiple HTTP requests.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Cookies<\/strong><\/li>\n\n\n\n<li><strong>Session State<\/strong><\/li>\n\n\n\n<li><strong>TempData<\/strong><\/li>\n\n\n\n<li><strong>Query strings<\/strong><\/li>\n\n\n\n<li><strong>Hidden fields<\/strong><\/li>\n\n\n\n<li><strong>HttpContext.Items<\/strong><\/li>\n\n\n\n<li><strong>Cache<\/strong><\/li>\n<\/ul>\n\n\n\n<p>24. Can ASP.NET Application be run in Docker containers?<\/p>\n\n\n\n<p>Yes, you can run an ASP.NET application or .NET Core application in Docker containers.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>For Docker interview questions visit&nbsp;<a href=\"https:\/\/www.qfles.com\/interview-question\/docker-interview-questions\">Docker Questions<\/a><\/li>\n\n\n\n<li>For more about .NET and Docker visit&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/dotnet\/core\/docker\/introduction\" target=\"_blank\" rel=\"noreferrer noopener\">.NET and Docker<\/a>&nbsp;and&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/aspnet\/core\/host-and-deploy\/docker\/building-net-docker-images?view=aspnetcore-5.0\">Docker images for ASP.NET Core<\/a><\/li>\n<\/ul>\n\n\n\n<p><strong>24. Explain Model Binding in ASP.NET Core.<\/strong><\/p>\n\n\n\n<p><span style=\"color: rgb(77, 81, 86); font-family: &quot;Google Sans&quot;, arial, sans-serif; font-size: 16px; white-space-collapse: collapse;\">Model binding&nbsp;<\/span><span style=\"background-color: rgba(80, 151, 255, 0.18); color: rgb(4, 12, 40); font-family: &quot;Google Sans&quot;, arial, sans-serif; font-size: 16px; white-space-collapse: collapse;\">allows controller actions to work directly with model types (passed in as method arguments), rather than HTTP requests<\/span><span style=\"color: rgb(77, 81, 86); font-family: &quot;Google Sans&quot;, arial, sans-serif; font-size: 16px; white-space-collapse: collapse;\">. Mapping between incoming request data and application models is handled by model binders<\/span><\/p>\n\n\n\n<p><strong>25. Explain Custom Model Binding.<\/strong><\/p>\n\n\n\n<p>You can apply the ModelBinder attribute to individual model properties (such as on a viewmodel) or to action method parameters to specify a certain model binder or model name for just that type or action<\/p>\n\n\n\n<p><strong>26. Describe Model Validation.<\/strong><\/p>\n\n\n\n<p>we will understand the concept of model validation in ASP.NET MVC core 3.1. It is valid for any version of MVC core. These validations are available in System.ComponentModel.DataAnnotations namespace. Validation attributes let us specify validation rules for model properties. Model state represents errors that come from two sub systems&#8217; model binding and model validation.<\/p>\n\n\n\n<p>There are in-built attributes in ASP.NET MVC core,<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Attribute<\/td><td>Description<\/td><\/tr><tr><td>CreditCard<\/td><td>This validates that the property has a credit card format.<\/td><\/tr><tr><td>Compare<\/td><td>This attribute validates that two property in model class match like password and compare password.<\/td><\/tr><tr><td>EmailAddress<\/td><td>This validates the property has email address format.<\/td><\/tr><tr><td>Phone<\/td><td>This validates that the property has a telephone number format.<\/td><\/tr><tr><td>Range<\/td><td>This validates that the property value within a specified range.<\/td><\/tr><tr><td>RegularExpression<\/td><td>This validates that the property value matches a specified regular expression.<\/td><\/tr><tr><td>Required<\/td><td>This validates that the field is not null<\/td><\/tr><tr><td>StringLength<\/td><td>This validates that a string property value doesn&#8217;t exceed a specified length limit.<\/td><\/tr><tr><td>Url<\/td><td>This validates that the property has a URL format.<\/td><\/tr><tr><td>Remote<\/td><td>This validates input on the client by calling an action method on the server<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>27. How to write custom ASP.NET Core middleware?<\/strong><\/p>\n\n\n\n<p>we can use this link to create custome middleware : <a href=\"https:\/\/www.c-sharpcorner.com\/article\/asp-net-core-custom-middleware\/\" data-type=\"URL\" data-id=\"https:\/\/www.c-sharpcorner.com\/article\/asp-net-core-custom-middleware\/\">https:\/\/www.c-sharpcorner.com\/article\/asp-net-core-custom-middleware\/<\/a><\/p>\n\n\n\n<p><strong>28. How to access HttpContext in ASP.NET Core?<\/strong><\/p>\n\n\n\n<p>HttpContext encapsulates all information about an individual HTTP request and response. An HttpContext instance is initialized when an HTTP request is received. The HttpContext instance is accessible by middleware and app frameworks such as Web API controllers, Razor Pages, SignalR, gRPC, and more<\/p>\n\n\n\n<p><strong>29. Explain the Change Token.<\/strong><\/p>\n\n\n\n<p>we can create change token by using the link : <a href=\"https:\/\/www.c-sharpcorner.com\/article\/using-change-tokens-in-net-7\/#:~:text=Introduction,to%20use%20change%20tokens%20in%20.\" data-type=\"URL\" data-id=\"https:\/\/www.c-sharpcorner.com\/article\/using-change-tokens-in-net-7\/#:~:text=Introduction,to%20use%20change%20tokens%20in%20.\">https:\/\/www.c-sharpcorner.com\/article\/using-change-tokens-in-net-7\/#:~:text=Introduction,to%20use%20change%20tokens%20in%20.<\/a><\/p>\n\n\n\n<p><strong>30. How to used ASP.NET Core APIs in class library?<\/strong><\/p>\n\n\n\n<p>we created a C# class library using the standard template and targeting net5.0. I then unloaded the project and edited the project file to read as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;Project Sdk=\"Microsoft.NET.Sdk.Web\"&gt;\n  &lt;PropertyGroup&gt;\n    &lt;TargetFramework&gt;net5.0&lt;\/TargetFramework&gt;\n    &lt;OutputType&gt;Library&lt;\/OutputType&gt;\n    &lt;NoDefaultLaunchSettingsFile&gt;true&lt;\/NoDefaultLaunchSettingsFile&gt;\n  &lt;\/PropertyGroup&gt;\n&lt;\/Project&gt;<\/pre>\n\n\n\n<p>when I reloaded the project Visual Studio was happy to load my class library project and it looked like this (just like an ASP.NET Web API project would look):<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/user-images.githubusercontent.com\/20465797\/114607798-a1061780-9ca5-11eb-81c2-c46fd472e10d.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" src=\"https:\/\/user-images.githubusercontent.com\/20465797\/114607798-a1061780-9ca5-11eb-81c2-c46fd472e10d.png\" alt=\"image\"\/><\/a><\/figure>\n\n\n\n<p>I was then able to test that ASP.NET APIs can be used by moving the following&nbsp;<code>Startup.cs<\/code>&nbsp;file into the class library:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">using Microsoft.AspNetCore.Builder;\nusing Microsoft.AspNetCore.Hosting;\nusing Microsoft.AspNetCore.Http;\nusing Microsoft.Extensions.DependencyInjection;\nusing Microsoft.Extensions.Hosting;\n\nnamespace AspNetClassLibTest\n{\n  public class StartupLib\n  {\n    public void ConfigureServices(IServiceCollection services) { }\n\n    public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {\n      if(env.IsDevelopment()) {\n        app.UseDeveloperExceptionPage();\n      }\n\n      app.UseRouting();\n\n      app.UseEndpoints(endpoints =&gt; {\n        endpoints.MapGet(\"\/\", async context =&gt; {\n          await context.Response.WriteAsync(\"Hello World from lib!\");\n        });\n      });\n    }\n  }\n}<\/pre>\n\n\n\n<p>I referenced the class library in my ASP.NET Web API project and it looked as follows:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/user-images.githubusercontent.com\/20465797\/114608212-1b369c00-9ca6-11eb-8754-fc0e0e4270ba.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" src=\"https:\/\/user-images.githubusercontent.com\/20465797\/114608212-1b369c00-9ca6-11eb-8754-fc0e0e4270ba.png\" alt=\"image\"\/><\/a><\/figure>\n\n\n\n<p>The&nbsp;<code>Program.cs<\/code>&nbsp;file references the&nbsp;<code>StartupLib<\/code>&nbsp;class defined in the class library:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">using Microsoft.AspNetCore.Hosting;\nusing Microsoft.Extensions.Hosting;\n\nnamespace AspNetClassLibTest\n{\n  public class Program\n  {\n    public static void Main(string[] args) {\n      CreateHostBuilder(args).Build().Run();\n    }\n\n    public static IHostBuilder CreateHostBuilder(string[] args) =&gt;\n        Host.CreateDefaultBuilder(args)\n            .ConfigureWebHostDefaults(webBuilder =&gt; {\n              webBuilder.UseStartup&lt;StartupLib&gt;(); \/\/ &lt;----- reference class from class library.\n            });\n  }\n}<\/pre>\n\n\n\n<p><strong>Running the Web API project produces the expected output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/user-images.githubusercontent.com\/20465797\/114608783-b92a6680-9ca6-11eb-9692-fc5ce45def1b.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" src=\"https:\/\/user-images.githubusercontent.com\/20465797\/114608783-b92a6680-9ca6-11eb-9692-fc5ce45def1b.png\" alt=\"image\"\/><\/a><\/figure>\n\n\n\n<p><strong>31. What is the Open Web Interface for .NET (OWIN)?<\/strong><\/p>\n\n\n\n<p>OWIN allows web apps to be decoupled from web servers. It defines a standard way for middleware to be used in a pipeline to handle requests and associated responses. ASP.NET Core applications and middleware can interoperate with OWIN-based applications, servers, and middleware.<\/p>\n\n\n\n<p>OWIN provides a decoupling layer that allows two frameworks with disparate object models to be used together. The&nbsp;<code>Microsoft.AspNetCore.Owin<\/code>&nbsp;package provides two adapter implementations:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>ASP.NET Core to OWIN<\/li>\n\n\n\n<li>OWIN to ASP.NET Core<\/li>\n<\/ul>\n\n\n\n<p>This allows ASP.NET Core to be hosted on top of an OWIN compatible server\/host or for other OWIN compatible components to be run on top of ASP.NET Core.<\/p>\n\n\n\n<p><strong>32. Describe the URL Rewriting Middleware in ASP.NET Core.<\/strong><\/p>\n\n\n\n<p>URL rewriting&nbsp;<strong>creates an abstraction between resource locations and their addresses so that the locations and addresses aren&#8217;t tightly linked<\/strong>. URL rewriting is valuable in several scenarios to: Move or replace server resources temporarily or permanently and maintain stable locators for those resources<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>33. Explain the Caching or Response caching in ASP.NET Core.<\/strong><\/p>\n\n\n\n<p><strong>Caching<\/strong>&nbsp;significantly improves the performance of an application by reducing the number of calls to actual data source. It also improves the scalability. Response caching is best suited for data that changes infrequently. Caching makes the copy of data and store it instead of generating data from original source.<br>Response caching headers control the response caching.&nbsp;<code>ResponseCache<\/code>&nbsp;attribute sets these caching headers with additional properties. For more visit&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/aspnet\/core\/performance\/caching\/overview?view=aspnetcore-6.0\" target=\"_blank\" rel=\"noreferrer noopener\">Caching in ASP.NET Core<\/a>.<\/p>\n\n\n\n<p><strong>34. What is In-memory cache?<\/strong><\/p>\n\n\n\n<p><strong>In-memory cache<\/strong>&nbsp;is the simplest way of caching by ASP.NET Core that stores the data in memory on web server.<br>Apps running on multiple server should ensure that sessions are sticky if they are using in-memory cache. Sticky Sessions responsible to redirect subsequent client requests to same server. In-memory cache can store any object but distributed cache only stores&nbsp;<code>byte[].<\/code><br><code>IMemoryCache<\/code>&nbsp;interface instance in the constructor enables the In-memory caching service via ASP.NET Core dependency Injection.<\/p>\n\n\n\n<p><strong>35. What is Distributed caching?<\/strong><\/p>\n\n\n\n<p>Applications running on multiple servers (Web Farm) should ensure that sessions are sticky. For Non-sticky sessions, cache consistency problems can occur.&nbsp;<strong>Distributed caching<\/strong>&nbsp;is implemented to avoid cache consistency issues. It offloads the memory to an external process. Distributed caching has certain advantages as below.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Data is consistent across client requests to multiple server<\/li>\n\n\n\n<li>Data keeps alive during server restarts and deployments.<\/li>\n\n\n\n<li>Data does not use local memory<\/li>\n<\/ul>\n\n\n\n<p><code>IDistributedCache<\/code>&nbsp;interface instance from any constructor enable distributed caching service via&nbsp;<a href=\"https:\/\/www.qfles.com\/interview-question\/solid-principles-interview-questions\">Dependency Injection<\/a>.<\/p>\n\n\n\n<p><strong>36. What is XSRF or CSRF? How to prevent Cross-Site Request Forgery (XSRF\/CSRF) attacks in ASP.NET Core?<\/strong><\/p>\n\n\n\n<p><strong>Cross-Site Request Forgery (XSRF\/CSRF)<\/strong>&nbsp;is an attack where attacker that acts as a trusted source send some data to a website and perform some action. An attacker is considered a trusted source because it uses the authenticated cookie information stored in browser.<br>For example a user visits some site &#8216;www.abc.com&#8217; then browser performs authentication successfully and stores the user information in cookie and perform some actions, In between user visits some other malicious site &#8216;www.bad-user.com&#8217; and this site contains some code to make a request to vulnerable site (www.abc.com). It&#8217;s called cross site part of CSRF.<br><strong>How to prevent CSRF?<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In ASP.NET Core 2.0 or later FormTaghelper automatically inject the antiforgery tokens into HTML form element.<\/li>\n\n\n\n<li>You can add manually antiforgery token in HTML forms by using&nbsp;<code>@Html.AntiForgeryToken()<\/code>&nbsp;and then you can validate it in controller by&nbsp;<code>ValidateAntiForgeryToken()<\/code>&nbsp;method.<\/li>\n\n\n\n<li>For more you can visit&nbsp;<a href=\"https:\/\/docs.microsoft.com\/en-us\/aspnet\/core\/security\/anti-request-forgery?view=aspnetcore-3.1\" target=\"_blank\" rel=\"noreferrer noopener\">Prevent Cross-Site Request Forgery (XSRF\/CSRF)<\/a><\/li>\n<\/ul>\n\n\n\n<p><strong>37. How to prevent Cross-Site Scripting (XSS) in ASP.NET Core?<\/strong><\/p>\n\n\n\n<p>validate all the input data, make sure that only the allowlisted data is allowed, and ensure that all variable output in a page is encoded before it is returned to the user<\/p>\n\n\n\n<p><strong>38. How to enable Cross-Origin Requests (CORS) in ASP.NET Core?<\/strong><\/p>\n\n\n\n<p><strong>39. What is the Area?<\/strong><\/p>\n\n\n\n<p><strong>Area<\/strong>&nbsp;is used to divide large ASP.NET MVC application into multiple functional groups. In general, for a large application Models, Views and controllers are kept in separate folders to separate the functionality. But Area is a MVC structure that separate an application into multiple functional groupings. For example, for an e-commerce site Billing, Orders, search functionalities can be implemented using different areas.<\/p>\n\n\n\n<p><strong>40. Explain the Filters.<\/strong><\/p>\n\n\n\n<p><strong>Filters<\/strong>&nbsp;provide the capability to run the code before or after the specific stage in request processing pipeline, it could be either MVC app or Web API service. Filters performs the tasks like Authorization, Caching implementation, Exception handling etc. ASP.NET Core also provide the option to create custom filters. There are 5 types of filters supported in ASP.NET Core Web apps or services.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Authorization filters<\/strong>&nbsp;run before all or first and determine the user is authorized or not.<\/li>\n\n\n\n<li><strong>Resource filters<\/strong>&nbsp;are executed after authorization.&nbsp;<code>OnResourceExecuting<\/code>&nbsp;filter runs the code before rest of filter pipeline and&nbsp;<code>OnResourceExecuted<\/code>&nbsp;runs the code after rest of filter pipeline.<\/li>\n\n\n\n<li><strong>Action filters<\/strong>&nbsp;run the code immediately before and after the action method execution. Action filters can change the arguments passed to method and can change returned result.<\/li>\n\n\n\n<li><strong>Exception filters<\/strong>&nbsp;used to handle the exceptions globally before wrting the response body<\/li>\n\n\n\n<li><strong>Result filters<\/strong>&nbsp;allow to run the code just before or after successful execution of action results.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Describe the ASP.NET Core. ASP.NET Core&nbsp;is an open-source, cross-platform and high performance platform that allows you to build modern, Internet-connected and cloud enabled applications. With ASP.NET Core you can 2. What are the benefits of using ASP.NET Core over ASP.NET? ASP.NET Core comes with the following benefits over&nbsp;ASP.NET. 3. What is the role of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[13],"tags":[],"class_list":["post-1793","post","type-post","status-publish","format-standard","hentry","category-asp-net-core"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"admin","author_link":"http:\/\/waqar-arshad.com\/index.php\/author\/waqar_29_1\/"},"uagb_comment_info":21,"uagb_excerpt":"1. Describe the ASP.NET Core. ASP.NET Core&nbsp;is an open-source, cross-platform and high performance platform that allows you to build modern, Internet-connected and cloud enabled applications. With ASP.NET Core you can 2. What are the benefits of using ASP.NET Core over ASP.NET? ASP.NET Core comes with the following benefits over&nbsp;ASP.NET. 3. What is the role of&hellip;","_links":{"self":[{"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/posts\/1793","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/comments?post=1793"}],"version-history":[{"count":3,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/posts\/1793\/revisions"}],"predecessor-version":[{"id":2167,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/posts\/1793\/revisions\/2167"}],"wp:attachment":[{"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/media?parent=1793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/categories?post=1793"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/tags?post=1793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}