ASP.NET Core Web API Interview

Dependency Injection, Services, EF Core, Async & Data Access

ASP.NET Core Web API interview questions covering dependency injection, services, EF Core, asynchronous programming and data access.

25 Questions Filter by level

Interview questions

Open a question for the full answer, code and interview guidance.

25 Results
101
Mid-LevelIntermediate

What is dependency injection in ASP.NET Core Web API?

Dependency injection (DI) is a design technique in which a class receives the services it depends on instead of constructing those services itself. ASP.NET Core includes a built-in dependency injection container.

102
Mid-LevelIntermediate

What are Transient, Scoped, and Singleton service lifetimes?

Transient services are created each time they are requested. Scoped services are created once per dependency-injection scope; in a typical HTTP request, that means one instance per request. Singleton services use one instance for the application's lifetime.

103
Mid-LevelIntermediate

Why is DbContext normally registered as a scoped service?

A DbContext represents a unit of work and tracks entity state. In a typical Web API, one scoped DbContext per request provides a natural boundary for querying and saving related changes.

104
Mid-LevelIntermediate

Should a controller contain business logic?

Controllers should mainly coordinate HTTP concerns: accept input, invoke application services, apply endpoint-level authorization, and translate outcomes into HTTP responses. Complex business workflows should generally live in application or domain services.

105
Mid-LevelIntermediate

What is the service layer in a Web API?

A service layer contains application use cases and coordinates business operations independently from the HTTP controller. It may validate business rules, invoke repositories or DbContext, call external services, and return application results.

106
Mid-LevelIntermediate

What is the repository pattern, and is it always required with EF Core?

A repository abstracts data-access operations behind an application-specific interface. It can be useful when the domain requires a meaningful persistence abstraction or when multiple data sources must be coordinated.

107
Mid-LevelIntermediate

What is the Unit of Work pattern in relation to EF Core?

Unit of Work groups a set of changes so they can be committed as one logical operation. EF Core DbContext naturally performs this role by tracking changes and persisting them through SaveChanges or SaveChangesAsync.

108
Mid-LevelIntermediate

Why should Web API data access use asynchronous methods?

Database and network operations are I/O-bound. Async APIs allow the request thread to be returned while the application waits for I/O, helping server throughput under concurrent workloads.

109
Mid-LevelIntermediate

What is the difference between Task and ValueTask in API code?

Task is the standard representation of an asynchronous operation and is appropriate for most Web API application code. ValueTask can reduce allocations in specialized cases where operations frequently complete synchronously, but it has more complex usage rules.

110
Mid-LevelIntermediate

Why should CancellationToken be accepted by API operations?

A CancellationToken lets work observe that the request has been cancelled, for example because the client disconnected or an upstream timeout occurred. Passing the token into EF Core and other asynchronous I/O can avoid wasting resources on work whose result is no longer needed.

111
Mid-LevelIntermediate

What is AsNoTracking in EF Core and when should an API use it?

AsNoTracking tells EF Core not to track returned entities for later updates. It is useful for read-only queries because it can reduce tracking overhead.

112
Mid-LevelIntermediate

Why should API queries project directly to DTOs?

Projection selects only the fields required by the response and lets EF Core translate that shape into SQL. It can reduce transferred columns, memory use, entity tracking, and accidental exposure of internal data.

113
Mid-LevelIntermediate

What is the N+1 query problem?

The N+1 problem occurs when one query loads a collection and then additional queries are executed separately for each item, producing many database round trips. This can cause severe performance degradation as result size grows.

114
Mid-LevelIntermediate

What is eager loading with Include in EF Core?

Eager loading retrieves related data as part of a query using Include and ThenInclude. It is useful when the application genuinely needs related entities as entities.

115
Mid-LevelIntermediate

What is the difference between eager, explicit, and lazy loading?

Eager loading retrieves related data as part of the original query. Explicit loading requests related data later through the DbContext. Lazy loading automatically queries related data when a navigation property is accessed, when configured.

116
Mid-LevelIntermediate

How should an API implement pagination with EF Core?

Apply deterministic ordering, then use Skip and Take for straightforward offset pagination. Validate page and page-size values and enforce a maximum page size.

117
Mid-LevelIntermediate

What is keyset or cursor pagination?

Keyset pagination requests records after a known ordered value instead of skipping an increasing number of rows. For example, the client may request products with Id greater than the last Id it received.

118
Mid-LevelIntermediate

How should filtering be implemented efficiently with EF Core?

Build an IQueryable and add predicates only for supplied, supported filters before materializing the query. This lets EF Core translate filtering to SQL so the database performs the work.

119
Mid-LevelIntermediate

How should dynamic sorting be handled safely?

Map client-supported sort names to known expressions instead of directly injecting arbitrary field names into SQL or accepting unrestricted expressions. Validate the requested direction and use a stable secondary ordering when necessary.

120
Mid-LevelIntermediate

What is optimistic concurrency in EF Core?

Optimistic concurrency assumes conflicts are uncommon and detects whether data changed between reading and saving. A concurrency token such as SQL Server rowversion can be checked when EF Core performs an update.

121
Mid-LevelIntermediate

When should an API use database transactions?

Use a transaction when multiple database operations must succeed or fail as one atomic unit and the default SaveChanges transaction boundary is not sufficient. EF Core already wraps a single SaveChanges call in a transaction for providers that support transactions.

122
Mid-LevelIntermediate

How should an API handle DbUpdateException?

DbUpdateException indicates a failure while persisting changes and may wrap provider-specific errors such as constraint violations. Log diagnostic information securely and translate only understood, expected cases into meaningful API responses.

123
Mid-LevelIntermediate

What is connection pooling and how does it affect Web APIs?

ADO.NET database providers normally pool physical database connections. Opening a connection typically obtains one from the pool and closing or disposing it returns it for reuse, reducing connection-establishment cost.

124
Mid-LevelIntermediate

How should external HTTP services be called from an ASP.NET Core Web API?

Use IHttpClientFactory or typed/named HttpClient registrations rather than repeatedly creating and disposing HttpClient instances per request. Factory-managed clients integrate with dependency injection and provide controlled handler lifetimes and configuration.

125
Mid-LevelIntermediate

What are best practices for services, EF Core, async, and data access in Web APIs?

Keep controllers focused on HTTP concerns and place application workflows behind well-defined services. Choose dependency-injection lifetimes carefully, keep DbContext request-scoped, propagate CancellationToken, and use asynchronous I/O for database and network operations.