Showing posts with label Middleware. Show all posts
Showing posts with label Middleware. Show all posts

Monday, November 10, 2025

Top 20 ASP.NET Core Interview Questions and Answers (2025 edition)

1. What is ASP.NET Core?

Answer:
ASP.NET Core is a cross-platform, high-performance, open-source framework developed by Microsoft for building modern, cloud-based, and internet-connected applications. It’s a redesign of ASP.NET and runs on .NET Core or .NET (5/6/7+).


2. What are the key differences between ASP.NET and ASP.NET Core?

Answer:

Feature ASP.NET ASP.NET Core
Platform Windows only Cross-platform
Hosting Model IIS IIS, Kestrel, Self-host
Performance Moderate High
Dependency Injection Limited Built-in
Configuration web.config appsettings.json

3. What is Middleware in ASP.NET Core?

Answer:
Middleware is software that’s assembled into the application pipeline to handle requests and responses. It can process requests before they reach the endpoint and after the response leaves it.

Example:

app.UseMiddleware<CustomMiddleware>();

4. What is Kestrel?

Answer:
Kestrel is a cross-platform web server for ASP.NET Core. It's the default web server and is designed for high performance.


5. Explain the Startup.cs file.

Answer:
Startup.cs is the entry point for configuring services and the app’s request pipeline.

  • ConfigureServices(IServiceCollection services): Registers services with DI.

  • Configure(IApplicationBuilder app, ...): Sets up middleware.


6. What is Dependency Injection (DI)?

Answer:
DI is a design pattern used to inject dependencies rather than creating them manually. ASP.NET Core has built-in DI support via the IServiceCollection.


7. How do you configure routing in ASP.NET Core?

Answer:
Routing is configured using endpoints:

app.UseRouting();

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
});

8. What is the difference between AddTransient, AddScoped, and AddSingleton?

Answer:

  • Transient: New instance every time.

  • Scoped: One instance per request.

  • Singleton: One instance for the app lifetime.


9. How is configuration handled in ASP.NET Core?

Answer:
ASP.NET Core uses configuration providers and reads from:

  • appsettings.json

  • Environment variables

  • Command-line arguments

Injected using:

public class MyClass(IConfiguration config) { }

10. What are Tag Helpers?

Answer:
Tag Helpers enable server-side code to participate in creating and rendering HTML elements in Razor views.

Example:

<a asp-controller="Home" asp-action="About">About</a>

11. How is logging handled in ASP.NET Core?

Answer:
ASP.NET Core uses built-in ILogger<T> and supports logging to:

  • Console

  • Debug

  • EventSource

  • External providers like Serilog, NLog


12. What is the Program.cs file in .NET 6/7/8 projects?

Answer:
Program.cs uses a minimal hosting model combining Startup and Program into one file.

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", () => "Hello World!");
app.Run();

13. How do you implement authentication and authorization?

Answer:

  • Authentication: AddAuthentication() and middleware

  • Authorization: [Authorize] attributes and policies

services.AddAuthentication(...);
services.AddAuthorization(...);

14. What is Razor Pages?

Answer:
Razor Pages is a page-focused web development model, simpler than MVC. Each .cshtml page has its own model (PageModel class).


15. How do you handle exceptions globally?

Answer:
Use UseExceptionHandler() or middleware.

app.UseExceptionHandler("/Home/Error");

Or custom middleware for API:

app.UseMiddleware<ErrorHandlingMiddleware>();

16. How can you create and consume Web APIs?

Answer:
Use [ApiController], and RESTful routing:

[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
    [HttpGet] public IActionResult GetAll() => Ok(...);
}

17. What is Model Binding in ASP.NET Core?

Answer:
Model binding maps incoming data (route, query string, form) to parameters or model objects.


18. What are Filters in ASP.NET Core?

Answer:
Filters are used to execute code before or after specific stages in the request pipeline:

  • Authorization

  • Action

  • Result

  • Exception


19. How is session management done?

Answer:
Enable session services and middleware:

services.AddSession();
app.UseSession();

Use:

HttpContext.Session.SetString("key", "value");

20. How do you implement caching?

Answer:

  • In-memory caching:

services.AddMemoryCache();
  • Use:

_cache.Set("key", data, TimeSpan.FromMinutes(5));

- Best of luck for interview

Prasad :)😇