Aspnet Core Interview Questions Middleware Di 2025 Interview Questions & Answers

29 questions available

Q1:

Explain the request-processing pipeline in ASP.NET Core from Kestrel to endpoint execution.

Mid

Answer

Pipeline execution flow: Kestrel accepts connection Request sent to IHttpApplication Middleware executed in order (Use ? Map ? Run) Endpoint routing middleware selects route Filters (for MVC/Minimal API) run Controller/Handler executes Response travels backward through middleware pipeline The pipeline is fully customizable.
Q2:

How does Middleware differ from Filters?

Mid

Answer

Middleware: Runs once per request, globally Works at pipeline level Can short-circuit requests Filters (MVC/Minimal API): Run after routing Specific to controllers/actions Ideal for validation, logging, caching
Q3:

What happens internally when you call app.Use() vs app.Run() vs app.Map()?

Mid

Answer

Use: Adds middleware and calls next() Run: Terminal middleware; no next() Map: Branches pipeline based on route/pattern
Q4:

Why is middleware order critical?

Mid

Answer

Because: Authentication must run before Authorization Exception handling must be at the top Static files must run before MVC CORS must run early Changing order changes behavior drastically.
Q5:

How does DI work internally in ASP.NET Core?

Mid

Answer

ASP.NET Core uses: Constructor injection Scoped container per request Service descriptors Activation via ActivatorUtilities Disposable services cleaned at request end A new service provider root is created during host startup.
Q6:

Why does ASP.NET Core avoid field injection and property injection?

Mid

Answer

To ensure: Strong constructor contract Testability Immutable services No hidden dependencies
Q7:

Explain Singleton vs Scoped vs Transient with real pipeline behavior.

Mid

Answer

Singleton: Same instance for entire application lifetime Scoped: New instance per request Transient: New instance on each resolution Scoped services resolve inside request scope only.
Q8:

Why is capturing scoped services inside singletons dangerous?

Mid

Answer

Because: Singletons live for entire app Scoped services live only for request Capturing scoped in singleton causes invalid references ? memory leaks, disposed objects
Q9:

What are Minimal APIs and how do they differ from MVC internally?

Mid

Answer

Minimal APIs: Lightweight endpoint metadata Use Endpoint Routing directly No controllers, filters, attributes Faster cold start Better for microservices MVC: Full action pipeline Filters, models, conventions
Q10:

How does ASP.NET Core model binding differ between Minimal APIs and MVC?

Mid

Answer

MVC: Uses complex model binders Handles form, route, query, headers, JSON Validation attributes Minimal APIs: Source binding via attributes: [FromRoute], [FromBody], [FromServices] Faster & simpler binding
Q11:

What is the difference between Endpoint Routing and MVC Routing?

Mid

Answer

Endpoint Routing (ASP.NET Core 3+): Routing happens early Endpoints stored in EndpointDataSource Middleware resolves endpoint before MVC executes.
Q12:

What is a Middleware Short-Circuit and why is it important?

Mid

Answer

Middleware can end pipeline early: if (!auth) return; Important for: Performance Security Static file serving Health checks.
Q13:

Why should you NOT throw exceptions for flow control in middleware?

Mid

Answer

Because exception handling middleware: Is expensive Logs errors unnecessarily Slows down hot paths Use Results or status codes instead.
Q14:

How does ASP.NET Core handle configuration fallback order?

Mid

Answer

Order of configuration providers: appsettings.json appsettings.{Environment}.json User secrets (dev) Environment variables Command-line arguments IConfigureOptions Last writer wins.
Q15:

What is the difference between IHostedService and BackgroundService?

Mid

Answer

IHostedService: Start/stop lifecycle Low-level control BackgroundService: Implements long-running ExecuteAsync() Handles cancellation tokens Ideal for queues, worker jobs.
Q16:

Why does ASP.NET Core not use SynchronizationContext like older ASP.NET?

Mid

Answer

Because: No thread affinity No deadlocks on async/await Task scheduler handles async work Higher throughput.
Q17:

What is the recommended way of handling exceptions globally?

Mid

Answer

Use: app.UseExceptionHandler("/error"); app.UseStatusCodePages(); Minimal API: app.MapExceptionHandler();
Q18:

How does ConfigureAwait(false) affect ASP.NET Core apps?

Mid

Answer

ASP.NET Core does NOT use SynchronizationContext, so: ConfigureAwait(false) does not change behavior But still improves performance in library code.
Q19:

What is the difference between HttpContext.Items and Session?

Mid

Answer

Items: Lives for a single request Session: Lives across requests, user-specific Items ideal for correlation data.
Q20:

How do you override model binding behavior globally?

Mid

Answer

Add custom model binders via: services.AddControllers(options => options.ModelBinderProviders.Insert(0, new CustomBinder())); This replaces or extends default binding for all controllers and actions.
Q21:

How does the Request Localization middleware work?

Mid

Answer

It resolves culture using configured providers: QueryStringRequestCultureProvider CookieRequestCultureProvider AcceptLanguageHeaderRequestCultureProvider Then sets CultureInfo.CurrentCulture and CurrentUICulture before the rest of the pipeline.
Q22:

What is Resource Filtering and how does it improve performance?

Mid

Answer

Resource filters wrap the entire MVC pipeline. They execute before model binding. They can short-circuit expensive steps like: Model validation Action execution Result execution Useful for caching and authorization.
Q23:

When should you use TypeActivatorCache in ASP.NET Core?

Mid

Answer

TypeActivatorCache precompiles constructors for: Controllers Middleware classes View components It avoids repeated reflection calls and improves object creation performance in heavy traffic apps.
Q24:

What is the difference between Authorization Policies and Authorization Handlers?

Mid

Answer

Authorization policies define requirements. Handlers evaluate those requirements per request. Policies = rule definitions Handlers = execution logic
Q25:

How does CORS middleware differ from proxy-level CORS?

Mid

Answer

CORS middleware validates browser-origin restrictions. Reverse proxies (Nginx, Apache) do not enforce CORS; they only forward headers. ASP.NET Core must use UseCors for browser-based protection.
Q26:

What is DefaultHttpContext and where is it used?

Mid

Answer

DefaultHttpContext is a lightweight HttpContext implementation. Used by Kestrel, Middleware, Testing, and object pooling. Helps minimize allocations in high-load servers.
Q27:

How do you log request and response bodies efficiently?

Mid

Answer

Enable buffering: context.Request.EnableBuffering() Copy the stream into memory, log it, then reset position. For responses: wrap body in a MemoryStream. Must avoid logging large payloads to prevent performance degradation.
Q28:

What is a Parameter Binding Source in Minimal APIs?

Mid

Answer

Binding sources define where values come from: Body, Route, Query, Header, Form, Services Minimal APIs infer binding automatically but can be overridden with explicit attributes.
Q29:

How do you detect slow endpoints in ASP.NET Core production environments?

Mid

Answer

Use: DiagnosticListener Middleware timing wrappers Application Insights RequestTelemetry EventCounters for queue length, requests/sec, thread pool usage Helps identify bottlenecks like slow database calls or blocking middleware.