Web API Final PDF
Web API Final PDF
3. What are HTTP verbs and how are they used in Web API?
Answer:
Ok() – 200 OK
BadRequest() – 400
NotFound() – 404
Created() – 201
7. How to enable CORS in ASP.NET Web API?
Answer:
CORS (Cross-Origin Resource Sharing) enables cross-domain requests. Enable it using:
8. What are media types and how does content negotiation work?
Answer:
Content negotiation determines the format (e.g., JSON or XML) of the response based on the
request’s Accept header.
Supports:
You generate the token on login, and use middleware like JwtBearerAuthentication to
validate it.
ExceptionFilterAttribute
Global handlers via config.Filters.Add(...)
Custom middleware (in .NET Core Web API)
14. What is throttling in Web API and how do you implement it?
Answer:
Throttling controls the number of API calls from a user/client/IP to avoid overloading.
Mock dependencies (e.g., services, repositories), call controller methods, and assert responses:
Use MultipartFormDataStreamProvider
Increase maxRequestLength in config
Use IFormFile (in .NET Core)
20. How to version APIs in Web API?
Answer:
21. What is the difference between ApiController and Controller in Web API?
Answer:
23. How do you send and receive JSON data in Web API?
Answer:
Use case:
Logging
Authentication
Compression
Register in WebApiConfig.
In .NET Core:
Inject in controller:
Examples:
JsonMediaTypeFormatter
XmlMediaTypeFormatter
Custom formatters (e.g., for CSV)
In .NET Core:
34. How do you return a file from Web API?
Answer:
37. What are API contracts and how do you define them?
Answer:
API contracts define expected request and response structure (data shape, status codes).
Defined using:
Models (DTOs)
Swagger/OpenAPI spec
XML comments for documentation
38. How do you test Web API using tools like Postman?
Answer:
You can also use route templates, constraints, and optional parameters.
Validation rules are applied using attributes like [Required], [Range], etc.
With [ApiController], this check is done automatically.
Used for:
Content negotiation
Documentation tools like Swagger
[Required]
[StringLength]
[Range]
Prevention:
Controllers – APIs
Services – Business logic
Repositories – Data access
Models/DTOs – Data contracts
Filters/Middleware – Cross-cutting logic
Utilities – Helpers, constants, etc.
Answer:
Answer:
Middleware is software that processes HTTP requests/responses in a pipeline.
Example:
63. How do you handle multiple API versions in ASP.NET Core Web API?
Answer:
Install:
Configure in Startup:
Use attributes:
Answer:
Describes the HTTP status codes and response types a method returns. Helps with Swagger
documentation.
65. How to implement global exception handling in Web API (Core)?
Answer:
Use middleware:
Answer:
Answer:
Type Purpose
ContentResult Returns plain text or HTML
JsonResult Returns data as JSON
Answer:
Answer:
Injects a service directly into an action method parameter.
Answer:
71. How can you return a custom error message in Web API?
Answer:
Or return detailed object:
You can also create custom exception classes and use global filters.
Answer:
Method Purpose
UseRouting() Builds route matching info
UseEndpoints() Executes matched endpoint
73. What are action result return types in ASP.NET Core Web API?
Answer:
IActionResult
ActionResult<T>
Ok(), BadRequest(), NotFound(), Created(), etc.
JsonResult, ContentResult, FileResult
Example:
Answer:
Answer:
Answer:
Answer:
Lifetime Description
Transient New instance every time
Scoped One instance per request
Singleton One instance for entire app life
78. What are DTOs and why are they used in Web API?
Answer:
Answer:
Answer:
Use HttpClient:
Answer:
Use IFormFile to receive uploaded files:
Set enctype="multipart/form-data" on client side.
Answer:
Type Description
IActionResult Generic interface for HTTP responses
ActionResult<T> Combines type-safe return + IActionResult
Answer:
Answer:
85. How do you return a CSV or Excel file from Web API?
Answer:
For Excel:
Answer:
Answer:
Answer:
Answer:
90. How do you set custom HTTP status codes in ASP.NET Core Web API?
Answer:
Answer:
Answer:
Types:
Answer:
Use [ResponseCache]:
Answer:
Answer:
96. How to log all requests and responses in ASP.NET Core Web API?
Answer:
Answer:
98. How can you secure sensitive configuration values (like API keys)?
Answer:
99. What is the difference between REST and SOAP Web Services?
Answer:
REST SOAP
Uses HTTP verbs (GET, POST) Uses XML over HTTP/SMTP
Lightweight and fast Heavy, strict contract
JSON/XML supported XML only
Stateless, cacheable Stateful, not easily cacheable
100. How can you enable CORS in Web API?
Answer: