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

Task Parallel Library (TPL) and Akka.NET: Differences

Task Parallel Library (TPL) and Akka.NET are both powerful tools in the .NET ecosystem for handling parallelism and concurrency, but they serve different purposes and use different models of computation. Here are some key differences:s 1.    Actor Model vs Task-Based Model: Akka.NET is built around the actor model, where actors are the fundamental units of computation and they communicate by exchanging messages. TPL, on the other hand, is task-based. It's designed to make developers more productive by simplifying the process of adding parallelism and concurrency to applications. TPL uses tasks (which are independently executing work units) and provides various ways to control and coordinate them. 2.    Fault Tolerance: One of the key features of Akka.NET is its built-in fault tolerance. It has a "let-it-crash" philosophy, where the system is designed to self-heal from errors. If an actor fails, its parent actor can decide on the supervision strategy: either to resta

Extension Methods - Advanced

Here we will see how can we use the Extension Methods in advanced manner in other types Imagine you often need to retrieve items from a List based on a custom criterion that the built-in LINQ methods don't cover. Extension Methods for Lists: Filtering based on Custom Criteria And the output would be   Extending Enums: Displaying Descriptive Strings Output: Extending DateTime: Calculating Age     Output: The code samples can be found at https://github.com/oneananda/C_Sharp_Examples/tree/main/ExtensionMethods