Aspnet Webapi Interview Questions: 25 Real-World Q&A from Production Manifests
Bite-sized, standalone interview questions and answers for Aspnet Webapi. Read 5-10 per sitting. Each answer is 2-4 sentences max and stands on its own.
25 questions shown. Filter by keyword or difficulty below.
Topic: ASP.NET WebAPI Fundamentals (Order 1)
Q: What is the structural difference between Minimal APIs and Controller-based APIs in ASP.NET Core? [Beginner]
Minimal APIs register endpoints directly as lambda delegates using MapGet, MapPost, etc., with no controller class, no method-level attributes, and no implicit model-binding conventions — every binding parameter, filter, and metadata is declared explicitly on the handler delegate. Controller-based APIs use attributed controller classes with [ApiController], [Route], and [HttpGet] on methods, relying on convention-based model binding, automatic 400 responses, and filter pipeline integration via ControllerBase. Minimal APIs produce simpler startup code but sacrifice structured organization, per-action filter granularity, and the ControllerBase helper surface.
Q: How does `WebApplication.CreateBuilder(args)` differ from the older `WebHost.CreateDefaultBuilder()` pattern? [Intermediate] </h3>
CreateBuilder returns a WebApplicationBuilder that merges the host-building and app-building phases into a single object — there is no separate Startup class, no ConfigureServices/Configure split, and the service collection is exposed directly on builder.Services while middleware is registered on the WebApplication returned by builder.Build(). The older pattern separated these concerns into two method groups on a Startup class, which made the pipeline’s shape harder to trace. Both ultimately produce the same IHost/IApplicationBuilder infrastructure under the hood.
</div>
Q: When would you choose Controllers over Minimal APIs despite the extra boilerplate? [Intermediate]
Controllers are the better choice when you need per-action filters (e.g., [ValidateAntiForgeryToken] on selected endpoints only), centralized exception filtering via [TypeFilter] or [ServiceFilter], or when your action methods share significant common logic through inheritance or ControllerBase helpers. Minimal APIs require per-handler middleware or custom IEndpointFilter implementations to replicate these patterns, which becomes cumbersome beyond ~10 endpoints.
## Topic: Middleware & Routing (Order 2)
{: .qa-topic }
Q: How does endpoint routing resolve which middleware runs before the endpoint handler? [Beginner]
UseRouting() places a middleware component that matches the incoming request against RouteEndpoint entries registered in EndpointDataSource, writing the match result into HttpContext.Features as an IEndpointFeature. Downstream middleware (authorization, CORS, response caching) read HttpContext.GetEndpoint() to make decisions, and UseEndpoints() dispatches to the matched endpoint’s RequestDelegate. Middleware placed before UseRouting() runs for every request unconditionally; middleware after it can branch based on which endpoint was matched.
Q: What determines the execution order of multiple middleware components? [Intermediate]
Order is determined solely by the sequence of app.Use(...) calls in Program.cs — the first registered middleware runs on the way in and the last on the way out after next() returns. There is no auto-ordering or priority system; middleware like UseAuthentication() must appear before UseAuthorization() because the auth middleware populates HttpContext.User that the authorization middleware then checks. The Build() method creates a nested RequestDelegate chain from last-registered (innermost) to first-registered (outermost).
Q: What does `app.UseStaticFiles()` do in terms of the middleware pipeline? [Beginner]
It adds a terminal middleware that short-circuits the pipeline when the request path matches a physical file under wwwroot. If a file is found, it writes the file content directly to the response and never calls next, so downstream middleware (including endpoint routing) never runs. If no file matches, UseStaticFiles calls next and the request continues normally. This is why static files should be placed early in the pipeline — before routing — to avoid unnecessary processing for static asset requests.
## Topic: Model Binding & Validation (Order 3)
{: .qa-topic }
Q: How does `[FromBody]` differ from `[FromQuery]` in binding behavior? [Intermediate]
[FromBody] reads the request body using an IInputFormatter selected by content-type negotiation — typically SystemTextJsonInputFormatter for JSON — deserializing the entire stream into the parameter type. [FromQuery] binds individual query string parameters by name using the model-binding system’s value providers, which only handle simple types and collections; complex types require [FromQuery] on a model class. [FromBody] can only bind one parameter per action because it consumes the request stream.
Q: How does FluentValidation integrate with ASP.NET Core's validation pipeline? [Intermediate]
FluentValidation provides an IValidator<T> interface whose Validate() method returns a ValidationResult with Errors. The FluentValidation.AspNetCore package registers an automatic validation filter that runs after model binding but before the action executes — it calls the registered IValidator<T> for the action parameter type and, on failure, short-circuits with a 400 response containing the validation errors in ProblemDetails format. This replaces the [Required]/[StringLength] data-annotation approach with a fluent, testable API.
## Topic: Dependency Injection (Order 4)
{: .qa-topic }
Q: How does scoped lifetime work per HTTP request in ASP.NET Core? [Intermediate]
The DI container creates one IServiceScope per HTTP request, managed by HttpContext.RequestServices as the scoped service provider. When a scoped service (e.g., AddScoped<IDbContext, AppDbContext>()) is resolved, a new instance is created within that scope and disposed when the scope is disposed at request end. Singleton services resolve once and live for the application lifetime; transient services resolve every time they are requested, even within the same scope.
Q: How does the DI container resolve open generics at runtime? [Intermediate]
AddScoped(typeof(IRepository<>), typeof(Repository<>)) registers an open generic — the container defers construction until resolution time, when it closes the generic with the requested type argument. When IRepository<Customer> is injected, the container calls typeof(Repository<>).MakeGenericType(typeof(Customer)) to produce Repository<Customer>, then resolves its constructor dependencies (which themselves may be open generics). This avoids registering each closed generic individually.
## Topic: Authentication & Authorization (Order 5)
{: .qa-topic }
Q: How does JWT Bearer authentication validate incoming tokens without calling an external service? [Intermediate]
AddJwtBearer() configures a handler that reads the Authorization: Bearer <token> header, parses the JWT, and validates it locally using the TokenValidationParameters — signature verification via the issuer’s JWKS endpoint (cached for AutomaticRefreshInterval), issuer/audience matching, lifetime checks (exp, nbf), and signing key rotation. No outbound call is made on every request because the public keys are fetched once and cached. The handler populates HttpContext.User with a ClaimsPrincipal if validation succeeds.
Q: How does policy-based authorization differ from role-based checks? [Intermediate]
Policy-based authorization decouples the authorization logic from the role name — a policy (e.g., "RequireAdmin") is defined in Program.cs with AddPolicy("RequireAdmin", p => p.RequireRole("Admin").RequireClaim("tenant")) and applied via [Authorize(Policy = "RequireAdmin")]. The policy evaluates one or more IAuthorizationRequirement instances via registered IAuthorizationHandler classes, which examine claims, roles, or any external state. Role-based checks ([Authorize(Roles = "Admin")]) are a special case of policy that only checks the role claim.
Q: What is the purpose of `IClaimsTransformation` in the auth pipeline? [Beginner]
IClaimsTransformation runs after authentication but before authorization, allowing you to augment, normalize, or translate the ClaimsPrincipal before policies are evaluated. A common use case is translating a database-stored role lookup into additional claims — the transformation receives the current principal and returns a new principal with added claims. It runs once per request because authentication middleware calls it via AuthenticationService.
## Topic: Versioning (Order 6)
{: .qa-topic }
Q: How do URL path versioning and header versioning differ in implementation? [Intermediate]
URL path versioning embeds the version in the route (e.g., /api/v1/orders) — ASP.NET API Versioning maps the version segment using [ApiVersion("1.0")] on the controller and a route template "api/v{version:apiVersion}/[controller]". Header versioning keeps a single URL and reads the version from a custom header (e.g., X-API-Version: 1.0) via IRequestReader; the reader extracts the version and selects the matching controller/action internally. URL versioning is visible and cache-friendly; header versioning keeps URLs stable but requires client cooperation.
Q: How does content negotiation versioning work with `IApiVersionSelector`? [Intermediate]
The client includes the API version in the Accept header as a custom media type parameter (application/json;v=1.0). ASP.NET API Versioning’s MediaTypeApiVersionReader parses this and selects the matching controller version. If multiple versions match (e.g., a controller exists for both v1.0 and v2.0), the IApiVersionSelector implementation (DefaultApiVersionSelector picks the lowest, LowestImplementedApiVersionSelector picks the highest, or custom) determines which controller handles the request.
## Topic: OpenAPI/Swagger (Order 7)
{: .qa-topic }
Q: How does Swashbuckle generate an OpenAPI document from ASP.NET Core endpoints? [Intermediate]
AddSwaggerGen() registers an ISwaggerProvider that reflects on registered controllers/endpoints using ApiDescriptionGroupCollectionProvider — the same provider the runtime uses for endpoint discovery. For each action, it extracts the route template, HTTP method, parameters, and return type, then walks XML comments (if IncludeXmlComments is configured) for /// <summary> and /// <param> descriptions. The result is serialized as an OpenAPI 3.0 JSON document at /swagger/v1/swagger.json.
Q: How does NSwag server-side code generation differ from client-side codegen? [Intermediate]
NSwag can generate both — server-side: it reads an OpenAPI document and produces ASP.NET Core controller stubs with [Route], [HttpPost], and parameter binding attributes, saving manual mapping of API contracts to code. Client-side: it reads the same document and generates a typed C# HTTP client (or TypeScript, etc.) that wraps HttpClient calls with serialization, error handling, and cancellation token support. The server generation is typically run at build time as a dotnet swagger tocode command; client generation runs from a nswag.json config file.
## Topic: Error Handling (Order 8)
{: .qa-topic }
Q: How does `UseExceptionHandler()` differ from `UseDeveloperExceptionPage()`? [Intermediate]
UseDeveloperExceptionPage() is a diagnostic middleware that captures detailed exception information — stack traces, query strings, cookies, headers — and renders an HTML page for local debugging. It must be placed early in the pipeline to catch exceptions from all downstream middleware. UseExceptionHandler("/error") is a production middleware that, on exception, re-executes the pipeline against a specified error endpoint path, allowing a controller action or Minimal API handler to return a consistent ProblemDetails JSON response instead of an HTML error page.
Q: How does `ProblemDetails` represent API errors in ASP.NET Core? [Intermediate]
ProblemDetails is a standardized response body defined by RFC 7807 — it carries type (a URI identifying the error type), title, status, detail, and instance fields. AddProblemDetails() registers middleware that catches unhandled exceptions and maps them to ProblemDetails with a 500 status, logging the full exception while returning only the safe detail. Controllers can return ValidationProblemDetails (inherits ProblemDetails with Errors dictionary) for 422 responses automatically via [ApiController] when model binding fails.
Q: What advantage does middleware-based error handling have over `IExceptionFilter`? [Intermediate]
Middleware-based error handling wraps the entire downstream pipeline, including endpoint routing, model binding, and action filters — it catches exceptions that occur before the filter pipeline even starts (e.g., a routing exception or a model-binding failure). IExceptionFilter only runs within the controller/action filter context and cannot catch exceptions from middleware registered before the filter pipeline. Middleware-based handling is the only way to guarantee every exception across the full request life cycle produces a consistent response.
## Topic: Performance (Order 9)
{: .qa-topic }
Q: How does output caching differ from response caching in ASP.NET Core? [Intermediate]
Output caching ([OutputCache]) stores the fully-rendered HTTP response in-memory on the server and serves it directly from middleware, bypassing the entire controller/endpoint execution — it is server-side and configurable per endpoint with vary-by-query, vary-by-header, and sliding expiration. Response caching ([ResponseCache]) sets standard Cache-Control, Expires, and Vary headers instructing downstream caches (browser, CDN, reverse proxy) to cache the response; the server still executes every request. Output caching reduces server load; response caching reduces network round trips.
Q: How does `System.Text.Json` minimize allocations in high-throughput serialization? [Intermediate]
System.Text.Json operates directly on ReadOnlySpan<byte> / Span<byte> via Utf8JsonWriter and Utf8JsonReader, avoiding string allocation overhead. Source generation (JsonSerializerContext) pre-generates serialization code at compile time, eliminating runtime reflection. Pooling ArrayBufferWriter<byte> through ArrayPool<byte>.Shared avoids per-request buffer allocations.
## Topic: Testing (Order 10)
{: .qa-topic }
Q: How does `WebApplicationFactory` enable integration testing of the full pipeline? [Intermediate] </h3>
WebApplicationFactory<T> creates an in-memory TestServer that hosts the full ASP.NET Core pipeline — middleware, routing, model binding, filters, and endpoint execution — using the same Program class as production. Tests call factory.CreateClient() to get an HttpClient pointed at this test server and issue real HTTP requests, receiving real HTTP responses. The factory allows overriding services (e.g., replacing EF Core with an in-memory provider) via ConfigureWebHost(), so the test exercises the real pipeline with controlled dependencies.
</div>
Q: How do you isolate integration tests when using EF Core with `WebApplicationFactory`? [Intermediate]
Override the DI registration in ConfigureWebHost(IWebHostBuilder builder) by calling builder.ConfigureServices(services => { services.RemoveDbContext<AppDbContext>(); services.AddDbContext<AppDbContext>(o => o.UseInMemoryDatabase("TestDb")); }). Each test creates a new factory (or resets the in-memory database via EnsureDeleted/EnsureCreated) to guarantee test isolation. The in-memory provider behaves like a real database for query and save operations but does not enforce relational constraints like foreign keys.
Q: How do you mock `HttpContext` for testing Minimal API handler logic? [Intermediate]
For handlers that access HttpContext through IHttpContextAccessor, mock the accessor: mock.Setup(x => x.HttpContext).Returns(new DefaultHttpContext()). For handlers that receive HttpContext as a parameter (Minimal API delegates), create a DefaultHttpContext with the desired request/response properties and pass it directly. This avoids instantiating the full server pipeline for unit-level handler tests.
---
**Last updated:** July 2026 | **Total Q&A:** 25 across Aspnet Webapi
[Back to Q&A Index](/dev-blog/qa/)
CreateBuilder returns a WebApplicationBuilder that merges the host-building and app-building phases into a single object — there is no separate Startup class, no ConfigureServices/Configure split, and the service collection is exposed directly on builder.Services while middleware is registered on the WebApplication returned by builder.Build(). The older pattern separated these concerns into two method groups on a Startup class, which made the pipeline’s shape harder to trace. Both ultimately produce the same IHost/IApplicationBuilder infrastructure under the hood.
Q: When would you choose Controllers over Minimal APIs despite the extra boilerplate? [Intermediate]
Controllers are the better choice when you need per-action filters (e.g., [ValidateAntiForgeryToken] on selected endpoints only), centralized exception filtering via [TypeFilter] or [ServiceFilter], or when your action methods share significant common logic through inheritance or ControllerBase helpers. Minimal APIs require per-handler middleware or custom IEndpointFilter implementations to replicate these patterns, which becomes cumbersome beyond ~10 endpoints.
Q: How does endpoint routing resolve which middleware runs before the endpoint handler? [Beginner]
UseRouting() places a middleware component that matches the incoming request against RouteEndpoint entries registered in EndpointDataSource, writing the match result into HttpContext.Features as an IEndpointFeature. Downstream middleware (authorization, CORS, response caching) read HttpContext.GetEndpoint() to make decisions, and UseEndpoints() dispatches to the matched endpoint’s RequestDelegate. Middleware placed before UseRouting() runs for every request unconditionally; middleware after it can branch based on which endpoint was matched.
Q: What determines the execution order of multiple middleware components? [Intermediate]
Order is determined solely by the sequence of app.Use(...) calls in Program.cs — the first registered middleware runs on the way in and the last on the way out after next() returns. There is no auto-ordering or priority system; middleware like UseAuthentication() must appear before UseAuthorization() because the auth middleware populates HttpContext.User that the authorization middleware then checks. The Build() method creates a nested RequestDelegate chain from last-registered (innermost) to first-registered (outermost).
Q: What does `app.UseStaticFiles()` do in terms of the middleware pipeline? [Beginner]
It adds a terminal middleware that short-circuits the pipeline when the request path matches a physical file under wwwroot. If a file is found, it writes the file content directly to the response and never calls next, so downstream middleware (including endpoint routing) never runs. If no file matches, UseStaticFiles calls next and the request continues normally. This is why static files should be placed early in the pipeline — before routing — to avoid unnecessary processing for static asset requests.
Q: How does `[FromBody]` differ from `[FromQuery]` in binding behavior? [Intermediate]
[FromBody] reads the request body using an IInputFormatter selected by content-type negotiation — typically SystemTextJsonInputFormatter for JSON — deserializing the entire stream into the parameter type. [FromQuery] binds individual query string parameters by name using the model-binding system’s value providers, which only handle simple types and collections; complex types require [FromQuery] on a model class. [FromBody] can only bind one parameter per action because it consumes the request stream.
Q: How does FluentValidation integrate with ASP.NET Core's validation pipeline? [Intermediate]
FluentValidation provides an IValidator<T> interface whose Validate() method returns a ValidationResult with Errors. The FluentValidation.AspNetCore package registers an automatic validation filter that runs after model binding but before the action executes — it calls the registered IValidator<T> for the action parameter type and, on failure, short-circuits with a 400 response containing the validation errors in ProblemDetails format. This replaces the [Required]/[StringLength] data-annotation approach with a fluent, testable API.
Q: How does scoped lifetime work per HTTP request in ASP.NET Core? [Intermediate]
The DI container creates one IServiceScope per HTTP request, managed by HttpContext.RequestServices as the scoped service provider. When a scoped service (e.g., AddScoped<IDbContext, AppDbContext>()) is resolved, a new instance is created within that scope and disposed when the scope is disposed at request end. Singleton services resolve once and live for the application lifetime; transient services resolve every time they are requested, even within the same scope.
Q: How does the DI container resolve open generics at runtime? [Intermediate]
AddScoped(typeof(IRepository<>), typeof(Repository<>)) registers an open generic — the container defers construction until resolution time, when it closes the generic with the requested type argument. When IRepository<Customer> is injected, the container calls typeof(Repository<>).MakeGenericType(typeof(Customer)) to produce Repository<Customer>, then resolves its constructor dependencies (which themselves may be open generics). This avoids registering each closed generic individually.
Q: How does JWT Bearer authentication validate incoming tokens without calling an external service? [Intermediate]
AddJwtBearer() configures a handler that reads the Authorization: Bearer <token> header, parses the JWT, and validates it locally using the TokenValidationParameters — signature verification via the issuer’s JWKS endpoint (cached for AutomaticRefreshInterval), issuer/audience matching, lifetime checks (exp, nbf), and signing key rotation. No outbound call is made on every request because the public keys are fetched once and cached. The handler populates HttpContext.User with a ClaimsPrincipal if validation succeeds.
Q: How does policy-based authorization differ from role-based checks? [Intermediate]
Policy-based authorization decouples the authorization logic from the role name — a policy (e.g., "RequireAdmin") is defined in Program.cs with AddPolicy("RequireAdmin", p => p.RequireRole("Admin").RequireClaim("tenant")) and applied via [Authorize(Policy = "RequireAdmin")]. The policy evaluates one or more IAuthorizationRequirement instances via registered IAuthorizationHandler classes, which examine claims, roles, or any external state. Role-based checks ([Authorize(Roles = "Admin")]) are a special case of policy that only checks the role claim.
Q: What is the purpose of `IClaimsTransformation` in the auth pipeline? [Beginner]
IClaimsTransformation runs after authentication but before authorization, allowing you to augment, normalize, or translate the ClaimsPrincipal before policies are evaluated. A common use case is translating a database-stored role lookup into additional claims — the transformation receives the current principal and returns a new principal with added claims. It runs once per request because authentication middleware calls it via AuthenticationService.
Q: How do URL path versioning and header versioning differ in implementation? [Intermediate]
URL path versioning embeds the version in the route (e.g., /api/v1/orders) — ASP.NET API Versioning maps the version segment using [ApiVersion("1.0")] on the controller and a route template "api/v{version:apiVersion}/[controller]". Header versioning keeps a single URL and reads the version from a custom header (e.g., X-API-Version: 1.0) via IRequestReader; the reader extracts the version and selects the matching controller/action internally. URL versioning is visible and cache-friendly; header versioning keeps URLs stable but requires client cooperation.
Q: How does content negotiation versioning work with `IApiVersionSelector`? [Intermediate]
The client includes the API version in the Accept header as a custom media type parameter (application/json;v=1.0). ASP.NET API Versioning’s MediaTypeApiVersionReader parses this and selects the matching controller version. If multiple versions match (e.g., a controller exists for both v1.0 and v2.0), the IApiVersionSelector implementation (DefaultApiVersionSelector picks the lowest, LowestImplementedApiVersionSelector picks the highest, or custom) determines which controller handles the request.
Q: How does Swashbuckle generate an OpenAPI document from ASP.NET Core endpoints? [Intermediate]
AddSwaggerGen() registers an ISwaggerProvider that reflects on registered controllers/endpoints using ApiDescriptionGroupCollectionProvider — the same provider the runtime uses for endpoint discovery. For each action, it extracts the route template, HTTP method, parameters, and return type, then walks XML comments (if IncludeXmlComments is configured) for /// <summary> and /// <param> descriptions. The result is serialized as an OpenAPI 3.0 JSON document at /swagger/v1/swagger.json.
Q: How does NSwag server-side code generation differ from client-side codegen? [Intermediate]
NSwag can generate both — server-side: it reads an OpenAPI document and produces ASP.NET Core controller stubs with [Route], [HttpPost], and parameter binding attributes, saving manual mapping of API contracts to code. Client-side: it reads the same document and generates a typed C# HTTP client (or TypeScript, etc.) that wraps HttpClient calls with serialization, error handling, and cancellation token support. The server generation is typically run at build time as a dotnet swagger tocode command; client generation runs from a nswag.json config file.
Q: How does `UseExceptionHandler()` differ from `UseDeveloperExceptionPage()`? [Intermediate]
UseDeveloperExceptionPage() is a diagnostic middleware that captures detailed exception information — stack traces, query strings, cookies, headers — and renders an HTML page for local debugging. It must be placed early in the pipeline to catch exceptions from all downstream middleware. UseExceptionHandler("/error") is a production middleware that, on exception, re-executes the pipeline against a specified error endpoint path, allowing a controller action or Minimal API handler to return a consistent ProblemDetails JSON response instead of an HTML error page.
Q: How does `ProblemDetails` represent API errors in ASP.NET Core? [Intermediate]
ProblemDetails is a standardized response body defined by RFC 7807 — it carries type (a URI identifying the error type), title, status, detail, and instance fields. AddProblemDetails() registers middleware that catches unhandled exceptions and maps them to ProblemDetails with a 500 status, logging the full exception while returning only the safe detail. Controllers can return ValidationProblemDetails (inherits ProblemDetails with Errors dictionary) for 422 responses automatically via [ApiController] when model binding fails.
Q: What advantage does middleware-based error handling have over `IExceptionFilter`? [Intermediate]
Middleware-based error handling wraps the entire downstream pipeline, including endpoint routing, model binding, and action filters — it catches exceptions that occur before the filter pipeline even starts (e.g., a routing exception or a model-binding failure). IExceptionFilter only runs within the controller/action filter context and cannot catch exceptions from middleware registered before the filter pipeline. Middleware-based handling is the only way to guarantee every exception across the full request life cycle produces a consistent response.
Q: How does output caching differ from response caching in ASP.NET Core? [Intermediate]
Output caching ([OutputCache]) stores the fully-rendered HTTP response in-memory on the server and serves it directly from middleware, bypassing the entire controller/endpoint execution — it is server-side and configurable per endpoint with vary-by-query, vary-by-header, and sliding expiration. Response caching ([ResponseCache]) sets standard Cache-Control, Expires, and Vary headers instructing downstream caches (browser, CDN, reverse proxy) to cache the response; the server still executes every request. Output caching reduces server load; response caching reduces network round trips.
Q: How does `System.Text.Json` minimize allocations in high-throughput serialization? [Intermediate]
System.Text.Json operates directly on ReadOnlySpan<byte> / Span<byte> via Utf8JsonWriter and Utf8JsonReader, avoiding string allocation overhead. Source generation (JsonSerializerContext) pre-generates serialization code at compile time, eliminating runtime reflection. Pooling ArrayBufferWriter<byte> through ArrayPool<byte>.Shared avoids per-request buffer allocations.
Q: How does `WebApplicationFactory` enable integration testing of the full pipeline? [Intermediate] </h3>
WebApplicationFactory<T> creates an in-memory TestServer that hosts the full ASP.NET Core pipeline — middleware, routing, model binding, filters, and endpoint execution — using the same Program class as production. Tests call factory.CreateClient() to get an HttpClient pointed at this test server and issue real HTTP requests, receiving real HTTP responses. The factory allows overriding services (e.g., replacing EF Core with an in-memory provider) via ConfigureWebHost(), so the test exercises the real pipeline with controlled dependencies.
</div>
Q: How do you isolate integration tests when using EF Core with `WebApplicationFactory`? [Intermediate]
Override the DI registration in ConfigureWebHost(IWebHostBuilder builder) by calling builder.ConfigureServices(services => { services.RemoveDbContext<AppDbContext>(); services.AddDbContext<AppDbContext>(o => o.UseInMemoryDatabase("TestDb")); }). Each test creates a new factory (or resets the in-memory database via EnsureDeleted/EnsureCreated) to guarantee test isolation. The in-memory provider behaves like a real database for query and save operations but does not enforce relational constraints like foreign keys.
Q: How do you mock `HttpContext` for testing Minimal API handler logic? [Intermediate]
For handlers that access HttpContext through IHttpContextAccessor, mock the accessor: mock.Setup(x => x.HttpContext).Returns(new DefaultHttpContext()). For handlers that receive HttpContext as a parameter (Minimal API delegates), create a DefaultHttpContext with the desired request/response properties and pass it directly. This avoids instantiating the full server pipeline for unit-level handler tests.
---
**Last updated:** July 2026 | **Total Q&A:** 25 across Aspnet Webapi
[Back to Q&A Index](/dev-blog/qa/)
WebApplicationFactory<T> creates an in-memory TestServer that hosts the full ASP.NET Core pipeline — middleware, routing, model binding, filters, and endpoint execution — using the same Program class as production. Tests call factory.CreateClient() to get an HttpClient pointed at this test server and issue real HTTP requests, receiving real HTTP responses. The factory allows overriding services (e.g., replacing EF Core with an in-memory provider) via ConfigureWebHost(), so the test exercises the real pipeline with controlled dependencies.
Q: How do you isolate integration tests when using EF Core with `WebApplicationFactory`? [Intermediate]
Override the DI registration in ConfigureWebHost(IWebHostBuilder builder) by calling builder.ConfigureServices(services => { services.RemoveDbContext<AppDbContext>(); services.AddDbContext<AppDbContext>(o => o.UseInMemoryDatabase("TestDb")); }). Each test creates a new factory (or resets the in-memory database via EnsureDeleted/EnsureCreated) to guarantee test isolation. The in-memory provider behaves like a real database for query and save operations but does not enforce relational constraints like foreign keys.
Q: How do you mock `HttpContext` for testing Minimal API handler logic? [Intermediate]
For handlers that access HttpContext through IHttpContextAccessor, mock the accessor: mock.Setup(x => x.HttpContext).Returns(new DefaultHttpContext()). For handlers that receive HttpContext as a parameter (Minimal API delegates), create a DefaultHttpContext with the desired request/response properties and pass it directly. This avoids instantiating the full server pipeline for unit-level handler tests.