All Interviews

07/Jul/2020 | 10 minutes to read

Best Answers to ASP.NET Core Interview Questions

QFLES is listing some Essential ASP.NET Core Interview Questions and Answers for Freshers and mid level of Experienced Professionals prepared by Industry Experts. All answers for these ASP.NET Core interview questions are given based on standard documents and tried to explain in simple and easiest way.

These interview questions are targeted for ASP.NET Core, ASP.NET Core MVC and Web API. All these questions are required to crack the .NET developer interview.

1. Describe the ASP.NET Core.

ASP.NET Core is a open-source, cross-platform and high performance platform that allow you to build modern, Internet-connected and cloud enabled applications. With ASP.NET Core you can

  • build web application, IoT (Internet of things) apps, services and mobile backends.
  • run on .Net Core.
  • You can do your development on Linux, Windows and MacOS.
  • deploy your code to cloud or on-premises.

2. What are the benefits of using ASP.NET Core over ASP.NET?

ASP.NET Core comes with the following benefits over ASP.NET.

  • Cross platform, provide ability to develop and run on Windows, Linux and MacOS.
  • Open-source
  • Unified Platform to develop Web UI and services.
  • Built-in dependency injection.
  • Ability to deploy on more than one server like IIS, Kestrel, Nginx, Docker, Apache etc
  • cloud enabled framework, provide support for environment based configuration system.
  • Lightweight, High performance and modern HTTP request pipelines.
  • well suited architecture for testability
  • Integration of many client-side frameworks like Angular
  • Blazor allow you to C# in browser with JavaScript.

3. What is the role of Startup class?

Startup class is responsible for configuration related things as below.

  • It configure the services which are required by app.
  • It defines the app's request handling pipeline as a series of middleware components.
// Startup class example
public class Startup 
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddRazorPages();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            app.UseHsts();
        }

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

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapRazorPages();
        });
    }
}
Startup class is specified inside 'CreateHostBuilder' method when host is created.
Multiple Startup classes can also be defined for different environments, At run time appropriate startup class is used.

4. What is the role of ConfigureServices and Configure method?

ConfigureServices method is optional and defined inside startup class as mentioned in above code. It's get called by host before 'Configure' method to configure the app's services.
Configure method is used to add middleware components to IApplicationBuilder instance that's available in Configure method. Configure method also specify how the app respond to HTTP request and response. ApplicationBuilder instance's 'Use...' extension method is used to add one or more middleware components to request pipeline.
You can configure the services and middleware components without Startup class and it's methods, by defining this configuration inside Program class in CreateHostBuilder method.

5. Describe the Dependency Injection.

Dependency Injection is a Design Pattern that's used as a technique to achieve the Inversion of Control (IoC) between the classes and their dependencies.
ASP.NET Core comes with built-in Dependency Injection framework that makes configured services available throughout the application. You can configure the services inside ConfigureServices method as below.

services.AddScoped<IMyDependency, MyDependency>();
A Service can be resolved using constructor injection and DI framework is responsible for the instance of this service at run time.

6. What problems Dependency Injection solve?

A class can use direct dependency instance as below.

Public class A {
MyDependency dep = new MyDependency();

public void Test(){
dep.SomeMethod();
}
}
But these direct dependencies can be problematic for following reasons.
  • If you want to replace 'MyDependency' with a different implementation then class must be modified.
  • It's difficult to Unit Test.
  • If MyDependency class has dependencies then it must be configured by class. If Multiple classes has dependency on 'MyDependency', the code becomes scattered.
DI framework solve these problems as below.
  • Use Interfaces or base class to abstract the dependency implementation.
  • Dependencies are registered in Service Container provided by ASP.NET Core inside Startup class 'ConfigureServices' method.
  • Dependencies are injected using constructor injection and instance is created by DI and destroyed when no longer needs.

7. Describe the Service Lifetimes.

When Services are registered, there is a lifetime for every service. ASP.NET Core provide following lifetimes.

  • Transient - Services with transient lifetime are created each time they are requested from service container. So it's best suited for stateless, light weight services.
  • Scoped - 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.
  • Singleton - Service with singleton lifetime is created once when first time the service is requested. For subsequent requests same instance is served by service container.

8. Explain the Middleware in ASP.NET Core.

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's called Terminal Middleware as It prevents next middleware from processing the request. You can add a middleware component to the pipeline by calling .Use... extension method as below.

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

    app.UseRouting();
So 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.

9. What is Request delegate?

Request delegates 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.

10. What is Host in ASP.NET Core?

Host 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:

  • HTTP Server implementation
  • Dependency Injection
  • Configuration
  • Logging
  • Middleware components

11. Describe the Generic Host and Web Host.

The host setup the server, request pipeline and responsible for app startup and lifetime management. There are two hosts:

  • .NET Generic Host
  • ASP.NET Core Web Host
.NET Generic Host is recommended and ASP.NET Core template builds a .NET Generic Host on app startup.
ASP.NET Core Web host is only used for backwards compatibility.
// Host creation
public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup();
}

12. Describe the Servers in ASP.NET Core.

Server is required to run any application. ASP.NET Core 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.
ASP.NET Core use the Kestrel web server by default. ASP.NET Core comes with:

  • Default Kestrel web server that's cross platform HTTP server implementation.
  • IIS HTTP Server that's in-process server for IIS.
  • HTTP.sys server that's a Windows-only HTTP server and it's based on the HTTP.sys kernel driver and HTTP Server API.

13. How Configuration works in ASP.NET Core?

In ASP.NET Core Configuration 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.

  • appsettings.json - settings file
  • Azure Key Vault
  • Environment variables
  • In-memory .Net objects
  • Command Line Arguments
  • Custom Providers
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. 'CreateDefaultBuilder' method provide default configuration.

14. How to read values from Appsettings.json file?

You can read values from appsettings.json using below code.

class Test{
// requires using Microsoft.Extensions.Configuration;
 private readonly IConfiguration Configuration;
    public TestModel(IConfiguration configuration)
    {
        Configuration = configuration;
    }
// public void ReadValues(){
var val = Configuration["key"]; // reading direct key values
var name = Configuration["Employee:Name"]; // read complex values
}
}
Default configuration provider first load the values from appsettings.json and then from appsettings.Environment.json file.
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.
You can also read the appsettings.json values using options pattern described Read values from appsettings.json file.

15. What is the Options Pattern in ASP.NET Core?

16. How to use multiple environments in ASP.NET Core?

17. How Logging works in .NET Core and ASP.NET Core?

18. How Routing works in ASP.NET Core?

19. How to handle errors in ASP.NET Core?

20. How ASP.NET Core serve static files?

23. Explain Session and State management in ASP.NET Core

24. Explain Model Binding in ASP.NET Core.

25. Explain Custom Model Binding.

26. Describe Model Validation.

27. How to write custom ASP.NET Core middleware?

28. How to access HttpContext in ASP.NET Core?

29. Explain the Change Token.

30. How to used ASP.NET Core APIs in class library?

31. What is the Open Web Interface for .NET (OWIN)?

32. Describe the URL Rewriting Middleware in ASP.NET Core.

33. Describe the application model in ASP.NET Core.

34. Explain the Caching or Response caching in ASP.NET Core.

35. What is In-memory cache?

36. what is distributed caching?

37. How to prevent Cross-Site Request Forgery (XSRF/CSRF) attacks in ASP.NET Core?

38. How to prevent Cross-Site Scripting (XSS) in ASP.NET Core?

39. How to enable Cross-Origin Requests (CORS) in ASP.NET Core?

18. What is the Area?

19. Explain the Filters.

Filters 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.

  • Authorization filters
  • Resource filters
  • Action filters
  • Exception filters used to handle the exceptions globally before wrting the response body
  • Result filters allow to run the code just before or after successful execution of action results.

20. Describe the View components in ASP.NET Core.

21. How View compilation works in ASP.NET Core?

22. Explain Buffering and Streaming approaches to upload files in ASP.NET Core.

Some General Interview Questions for ASP.NET Core:

1. How much will you rate your self in ASP.NET Core?

When you attend an interview, Interviewer may ask you to rate your self in specific Technology like ASP.NET Core, So It's depend on your knowledge and work experience in ASP.NET Core.

2. What challenges did you face while working on ASP.NET Core?

This question may be specific to your technology and completely depends on your past work experience. So you need to just explain the challenges you faced related to ASP.NET Core in your Project.

3. What was your role in last Project related to ASP.NET Core?

It's based on your role and responsibilities assigned to you and what functionality you implemented using ASP.NET Core in your project. This question is generally asked in every interview.

4. How much experience do you have in ASP.NET Core?

Here you can tell about your overall work experience on ASP.NET Core.

5. Have you done any ASP.NET Core Certification or Training?

It's depend on candidate like you have done any ASP.NET Core training or certification. Certifications or trainings are not essential but good to have.

Conclusion:

We have covered some frequently asked ASP.NET Core Interview Questions and Answers to help you for your Interview. All these Essential ASP.NET Core Interview Questions are targeted for mid level of experienced Professionals and freshers.
While attending any ASP.NET Core Interview if you face any difficulty to answer any question please write to us at [email protected]. Our IT Expert team will find the best answer and will update on portal. In case if we find any new ASP.NET Core questions, we will update the same here.