logo

,

15 Most Popular Asp.net Core Interview Questions And Answers

1) What is Asp.net Core?

Asp.net Core is a web development framework for building web applications on the .Net platform.
Asp.net Core is open source and can run on macOS, Linux, and Windows.

 

2) What is MVC?

MVC (Model – View – Controller) is an architectural pattern that separates an application into 3 main logical components, namely the Model, the View, and the Controller. Each component is built to handle specific development aspects of an application. MVC is the most popular industry-standard for web applications development.

 

3) What are the advantages of Asp.net Core?

  1. Command-Line support to build and run applications
  2. No web.config file. We have to store our configuration information in the appSettings.json file.
  3. No Global.asax file. We can register and use the services from the startup.cs file
  4. Built-in support for Dependency Injection.
  5. Built-in support for logging
  6.  Cross-platform
  7. Ability to deploy on more than one server like IIS, Docker, Apache, etc.
  8. Provides good support for asynchronous programming
  9. Supports Web Sockets and SignalR
  10. Protects against CSRF (Cross-Site Request Forgery)

 

4) What is Dependency Injection in Asp.net Core?

It is one of the design patterns.

It can be defined as passing dependency to other objects or frameworks (dependency injector). Dependency injection makes testing easier.

It can be done through the constructor.

 

5) What is the Startup class in Asp.net Core?

The Startup class configures the services available to the .NET Dependency Injection (DI) container in the ConfigureServices() method, and configures the .NET request pipeline for the application in the Configure() method. Both methods are called by the .NET runtime when the app starts, first ConfigureServices() followed by Configure().

Read:  What Happened to Startup.cs File in .Net 6?

 

6) Explain Middleware in Asp.net Core.

Middleware is a software component that is assembled into an application pipeline to handle requests and responses.
Each component chooses whether to pass the request on to the next component in the pipeline and can perform certain actions before and after the next component is invoked in the pipeline.

 

7) How to enable Session in Asp.net Core?

Microsoft.AspNetCore.Session package provides middleware to manage the sessions in ASP.NET Core. We need to install this package in our project.

Next, we need to call the “AddSession” method in the ConfigureServices method of the startup class.
The “AddSession” method has one overload method, which accepts various session options such as Idle Timeout, Cookie Name, Cookie Domain, etc.

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddSession(options => { options.IdleTimeout = TimeSpan.FromMinutes(60); });
        }

Now, in the same class, we add “app.Usersession()” inside the configure function so that it gets called by the runtime.

 

public void Configure(IApplicationBuilder app, IHostingEnvironment env)  
{  
    if (env.IsDevelopment())  
    {  
        app.UseDeveloperExceptionPage();  
        app.UseBrowserLink();  
    }  
    else  
    {  
        app.UseExceptionHandler("/Home/Error");  
    }  
  
    app.UseStaticFiles();  
  
    app.UseSession();  
  
    app.UseMvc(routes => 
    {  
        routes.MapRoute(  
            name: "default",  
            template: "{controller=Home}/{action=Index}/{id?}");  
    });  
}

 

8) What are the different JSON files in the Asp.net Core application?

  1. global.json
  2. launchsettings.json
  3. appsettings.json
  4. bundleconfig.json
  5. bower.json
  6. package.json

 

9) What is the purpose of the wwwroot folder in Asp.net Core?

The wwwroot folder contains all static files like CSS, Images, JavaScript, Html files which are served directly to the clients.

 

10) What are Razor Pages in Asp.net Core?

Razor Pages is a new aspect of ASP.NET Core MVC introduced in ASP.NET Core 2.0. It offers a “page-based” approach for building server-side rendered apps in ASP.NET Core and can coexist with “traditional” MVC or Web API controllers.

 

11) Explain Transient, Singleton and Scoped lifetime services.

Transient lifetime services are created each time they are requested. This lifetime works best for lightweight, stateless services.

Singleton creates a single instance throughout the application. It creates the instance for the first time and reuses the same object in all calls.

Scoped lifetime services are created once per request within the scope. It is equivalent to a singleton in the current scope. For example, in MVC it creates one instance for each HTTP request, but it uses the same instance in the other calls within the same web request.

 

12) Explain Conventional and Attribute based routing in Asp.net Core?

Conventional Routing establishes a convention for the URL path, such that given a template:

  • First token maps to a controller
  • Second token maps to an action
  • Third token maps to an optional id action parameter

"{controller=Home}/{action=Index}/{id?}"

Attribute Routing maps URLs by applying routing template directly on the controller and action. The existence of controller and action in the template is not mandatory for attribute routing as it doesn’t play any part in the routing process and it is recommended that you use this approach for most of your APIs. Furthermore, it will make your code more explicit and reduce routing errors that might be introduced as you add more controllers and actions.


[Route("api/Products")]
[ApiController]
public class ProductsController : Controller
{
...
}

13) How to enable Cross-Origin Requests (CORS) policy in Asp.net Core?

1) Install CORS NuGet Package

PM> Install-package Microsoft.AspNetCore.Cors

2) Register CORS service in the configure services method in the Startup.cs file as below:


public void ConfigureServices(IServiceCollection services)
{
    services.AddCors();
    services.AddControllersWithViews();
}

3) Enable CORS using middleware Add CORS middleware in the configure method in the Startup.cs file as below:


public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
        app.UseHsts();
    }
 
    app.UseHttpsRedirection();
 
    // Shows UseCors with CorsPolicyBuilder.
    app.UseCors(builder =>
    {
        builder
        .AllowAnyOrigin()
        .AllowAnyMethod()
        .AllowAnyHeader();
    });
 
    app.UseStaticFiles();
 
    app.UseRouting();
 
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");
    });
}

14) What are the different return types of controller action methods?

  1. ViewResult
  2. PartialView
  3. Redirect
  4. RedirectToAction
  5. Content
  6. JSON
  7. JavaScript
  8. File

 

15) What is Content Negotiation in Asp.net Core Web API?

It is a mechanism where the client should be able to decide the format of response from the server like if the client wants the response in either xml, json, html etc.
For more visit Content Negotiation in Asp.net Core Web API

One Response

Leave a Reply

Your email address will not be published.

Share on facebook
Share on twitter
Share on linkedin

Related articles