Skip to main content

ASP.NET Core Interview Questions : July 2022

1) What is appsettings.json?

This .json file is holding the application configuration values for a.net core application, this is very similar to web.config or app.config in a legacy .net application 

We can have multiple versions of the appsettings.json file

Template: appsettings.[Environment].json

Example: 

* appsettings.DevEnv.json
* appsettings.TestEnv.json
* appsettings.Staging.json
* appsettings.json 

Little deeper:

In order to load the configuration information into the application from the file we need the following class in the .net : JSON Configuration Provider

Inherited from FileConfigurationProvider

How to use:

var _ConfigurationBuilder = new ConfigurationBuilder(Environment.ApplicationBasePath)

              .AddJsonFile("appsettings.Live.json")                .AddEnvironmentVariables();

Configuration = _ConfigurationBuilder.Build();

 

2) What is the difference between ASP.NET CORE with .NET Framework and ASP.NET with .NET Core Framework

ASP.NET CORE with .NET Framework:

This is purely based on Windows OS, any application based on this framework will work only in Windows OS.

ASP.NET CORE with .NET Core Framework

Contrast to the previous one this is platform independent, means not bound to any OS like Linux, Windows, Mac OS.


3) How to Get the username of the logged user in ASP.NET Core?

Assuming that the application is set to work in Windows Authentication in the launchSettings.json file

"windowsAuthentication": true

 

 

private readonly IHttpContextAccessor _httpContextAccessor;

var username = _httpContextAccessor.HttpContext.User.Identity.Name;

 

4) How to find the IP Address of the client machine

Without Proxy: Using HttpContext.Connection Property

HttpContext context; // Need to initialize

var IPAddress = context.Connection.RemoteIpAddress;

 

With Proxy we need some workaround: Please see this link for complete steps

 

5) How to get the query string in the method in ASP.NET Core

 var NameQueryString = HttpContext.Request.Query["Name"];

 

6) What is response buffering in ASP.NET Core?

A large response, usually a file download from the server will take a normal route of streaming the file by dividing it into small chunks and send it to user, as oppose to streaming the whole file will be loaded into the memory to serve as it as to the user

Enabling / Disabling Response Buffering 

context.Request.EnableBuffering();

HttpContext.Features.Get<IHttpResponseBodyFeature>().DisableBuffering();


6) What is the default port for ASP.NET Core?

Answer: 5000

7) In which scenarios the port 5001 is used?

Answer: If the local certificate for secure http (https) is available in the development machine the 5001 port is used

8) Where can I specify the port?

Answer: In appsettings.json file

Example :

{

Kestrel:{
“endpoints”:{

“http”:{

url:”http://localhost:5000”

}

}

}

}

9) What is Kestral?

Answer: In short Kestral is a open source server which runs the ASP.NET Core in any platform

10)Is http:localhost:5000/MyPage is equal to http:localhost:5000/mypage?

Answer: Usually yes until we explicitly set the routing options for the service to lowercase only serviveObj.AddRouting(options => options.LowerCaseUrls = true);


11)And what is the use of setting the LowerCaseUrls?

 Answer: Sticking only to a single case (upper of lower URLs) will help the search engine to index the site / page better

The same site

 

http://mysite.com/help

and


http://MySite.com/Help 

 

both will be indexed as 2 separate pages in the search engines and search landing will be affected by this, so better to stick to one case option, preferably to lower case URL


12)What is the difference between IOC and DI?

 Answer: IOC (Inversion of control) is the design target intended to achieve between the classes and their dependencies,

DI (Dependency Injection) is one of the design patterns used to achieve this target

There are other ways to achieve IOC which is subjected to the application type

 

Comments

Popular posts from this blog

Using of global variables in C# - Drawbacks & Solutions

How using global variables can have implications on the design, maintainability, and test-ability of C# code: Harder to understand and reason about the code:       class Program     {         public static int globalCounter = 0;         static void Main()         {             globalCounter++;             Console.WriteLine(globalCounter);         }     }   In this example, the global variable globalCounter is accessible from anywhere in the program, including the Main method. It's not clear where the value of the globalCounter is updated, it could be updated in other methods or classes, making it harder to trace the flow of data and understand the source of bugs.   More prone to errors:       class Program     {         public static string globalString;         static void Main()         {             globalString = "Hello" ;             Method1();             Method2();         }         static void Method1()         {

Task Parallel Library (TPL) and Akka.NET Alternatives

Task Parallel Library (TPL) and Akka.NET are among the most commonly used libraries for parallel and concurrent programming in the .NET ecosystem. However, there are also several other options available, depending on your specific needs: Parallel Language Integrated Query (PLINQ) is a parallel programming feature of .NET that provides an easy and efficient way to perform operations on collections in parallel. LINQ (Language Integrated Query) is a powerful feature in .NET that allows developers to work with data in a more declarative and language-integrated manner. While LINQ queries are inherently sequential, PLINQ extends LINQ by providing parallel versions of the query operators, allowing some queries to execute faster by utilizing multiple processors or cores on a machine. PLINQ is great when you are working with large collections where operations might be CPU-intensive or I/O-bound and could potentially be sped up by parallel execution. Here is a simple example of a PLI

SOLID Principles with Real World examples in C#

  SOLID Principles with Real World examples in C#   SOLID principles are formed by using S Single Responsibility Principles (SRP) O Open Closed Principle (OCP) L Liskov’s Substitution Principle (LCP) I Interface Segregation Principle (ISP) D Dependency Inversion Principle (DIP)   S Single Responsibility Principles (SRP) There should never be more than one reason for a class to change, to be precise one class should have only one responsibility Single Responsibility Principles (SRP) Real world example, A perfect match for SRP is Microservices , a Microservice will not contain functionalities other than the one it is designated to do,  Example ·                   Order Processing Service, ·                   Shipment Management Service, ·                   User Authentication Service, ·                   Catalogue List Service       class OrderProcessor     {         public void Process(Order order)         {             // Check inven