Tuesday, September 1, 2020

Top 20 .NET Core Interview Questions & Answers You Need to Know

Top 20 .NET Core Interview Questions & Answers You Need to Know



1) What is .NET Core?

.NET Core is a free and open-source, managed computer software framework for Windows, Linux, MacOS operating systems. It is suitable for building cloud based internet connected applications like web apps, IoT apps and mobile apps. ASP.NET Core apps can run on .NET Core or on full .NET framework.

2) What are the features of .NET Core?

Flexible Deployment: Can be included in your app or installed side-by-side user or machine-wide.
Cross-Platform: Run on Windows, MacOS and Linux can be ported to other OSes. The supported operating systems, CPUs and application scenarios will grow over time, provided by Microsoft, other companies, and individuals.
Command-Line Tools: All product scenarios can be exercised at the command-line.
Compatible: .NET Core is compatible with .NET Framework, Xamarin and Mono, via the .NET Standard Library.
Open Source: The .NET Core platform is open source, using MIT and Apache 2 licenses and documentation is licensed under CC-BY.

3) What is Middleware?

Middleware is software that’s assembled into an app pipeline to handle requests and responses. Each component: • Chooses whether to pass the request to next component in pipeline. • Can perform work before and after the next component in pipeline. Request delegates are used to build the request pipeline. The request delegates handle each HTTP request.

4) What is Krestrel?

Krestrel is a cross-platform web server for ASP.NET Core based on libuv, a cross-platform asynchronous I/O library. It is secure and good enough to use it without a reverse proxy server. You can set a krestrel server in the program.cs file.


 

public class Program

{

public static void Main(string[] args)

{

    var host = new WebHostBuilder()

        .UseContentRoot(Directory.GetCurrentDirectory())

        .UseKestrel()

        .UseIISIntegration()

        .UseStartup<Startup>()

        .ConfigureKestrel((context, options) =>

        {

            // Set properties and call methods on options

        })

        .Build();

    host.Run();

}

}

 


5) What is tag helper in ASP.NET Core?

ASP.NET Core provides an in-build tag helper, Tag helper enables the server-side code that creates and renders the HTML elements in the razor page.

Example : <input asp-for="Email" class="form-control" />

  6) What are the various JSON files in ASP.NET Core?
 • Global.json
 • Launchsettings.json
 • appsettings.json
 • bundleconfig.json
 • bower.json
 • package.json

7) What is startup class ASP.NET Core?

Startup class is the entry point of any ASP.NET Core application, this class contains the application configuration related details, and it has two main methods ConfigureServices() and Configure().

 
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddMvc().AddRazorOptions(opt =>
            {
                opt.PageViewLocationFormats.Add("/Pages/shared/{0}.cshtml");
            });
        }
     public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
        }
    }

 

8) How to enable session in ASP.NET Core?

The middleware for the session is provided by Microsoft.AspNetCore.Session. To use the session in ASP.NET Core application we need to add the middleware in pipeline.

 

public class Startup

 {

     public void ConfigureServices(IServiceCollection services)

     { 

         services.AddSession();

         services.AddMvc();

     }

     public void Configure(IApplicationBuilder app, IHostingEnvironment env)

     {

        app.UseSession();

     }

  }

 

9) What is WebListener?

WebListener is a web server for ASP.NET Core that that runs only on Windows, based on the Windows Http Server API. It runs directly on the Http.Sys kernel driver. WebListener can be used for direct connection to the internet without relying on IIS. It can only be used independently.

10) What is Metapackage in ASP.NET Core?

In ASP.NET Core project we don’t need to include individual package references in the .csproj file. There is one file called Microsoft.AspNetCore.All is added under NuGet, Now Microsoft.AspNetCore.All contains all the required packages. That’s the concept of metapackage.

11) What is a 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 feature of ASP.NET Core.

Example: 
 @page
<h1> Hello</h1> 
 <h2> Razor Pages </h2>

12) What is launch.settings.json file in ASP.NET Core?

It holds the project-specific configuration to launch the application including the environment variable.


//launchSettings.json

{

  "iisSettings": {

    "windowsAuthentication": false,

    "anonymousAuthentication": true,

    "iisExpress": {

      "applicationUrl": "http://localhost:63977",

      "sslPort": 44389

    }

  },

  "profiles": {

    "IIS Express": {

      "commandName": "IISExpress",

      "launchBrowser": true,

      "environmentVariables": {

        "ASPNETCORE_ENVIRONMENT": "Development"

      }

    },

  "TestApp": {

      "commandName": "Project",

      "launchBrowser": true,

      "applicationUrl": "https://localhost:5001;http://localhost:5000",

      "environmentVariables": {

        "ASPNETCORE_ENVIRONMENT": "Development"

      }

    }

  }

}

 

 

13) How automatic model binding in Razor pages works?

When we create a razor page, it provides the option to have model binding or not, in model binding there is a model created with the razor page, the model is inherited from: PageModel we need to use [BindingProperty] attribute to bind any property to model.


 

public class ContactModel : PageModel

 {

     [BindProperty]

     public string Email { get; set; }

 }

 

 

14) How to disable Tag Helper at element level?

We can disable Tag Helper at element level using the opt-out character (“!”). This character must apply opening and closing HTML tag. Example: </!span>

15) How many types of single page application templates provided by the .NET Core?

      Angular

      React

      React with Redux

      Javascript Services

16) What is the ConfigureServices method of Startup.cs?

ConfigureServices method gets call runtime to register services to DI container. After registering the dependent classes, you can use those clastyle="font-size:2.0rem;"sses anywhere in the application. The ConfigureServices method includes the IServiceCollection parameter to register services to the DI container.

17) What is the Configure method of Startup.cs?

Configure the method gets call runtime to configure the HTTP request pipeline for application. Configure use the instance of the IApplicationBuilder that is provided by In-Build IOC container. In other words, you add the middleware in this method like routing and other custom middleware as per your application requirements.

18) Dependency injection in .NET Core

The .NET Core has an In-build dependency mechanism. You have to add namespace Microsoft.Extensions.DependencyInjection in Startup.cs file to get enable IServiceCollection. In the ConfigureServices method, you can add your decencies to a container that will automatically resolve your dependencies and you can access your classes anywhere in the application. The built-in IoC container supports three kinds of lifetimes: Singleton: It creates and shares a single instance of service throughout the application's lifetime. Transient: It creates a new instance of the specified service type every time when it requested. This lifetime works best for lightweight, stateless services. Scoped: It creates an instance of the specified service type once per request and will be shared in a single request.

19) How to handle errors in Middleware?

You can capture synchronous and asynchronous exception instances from the middleware pipeline and generate an HTML response. Following code has if the condition in which you can call different methods for development, staging, production environment or you can pass the environment name in IsInvironment extension method.

Also, you can configure a custom error handling page to call the UseExceptionHandler method that adds a middleware to a pipeline that will catch the exception, log them, Re-executes the request in an alternate pipeline for the page or controller indicated. The request isn't re-executed if the response has started.

 

if (env.IsDevelopment())

{

   app.UseDeveloperExceptionPage();

}

else

{

   app.UseExceptionHandler("/Error");

   app.UseHsts();

}

 

20) Does real-time application support by .NET Core?

Yes, Asp.net's core signalR is an open-source library that gives real-time web functionality to the application.