DotNet Core Web API Interview Question
- Get link
- X
- Other Apps
Qustions;
- What is Web API?
- What is REST?
- Web API vs WCF?
- Stateless vs Stateful Protocols?
- What are HTTP Verbs?
- GET vs POST?
- PUT vs PATCH?
- What is the Base class of Api Controller?
- What is API Endpoints?
- What is API Versioning?
- What is Minimal API?
- What is Content Negotiation?
- What are Return Types in Web API?
- What is Dependency Injection?
- What are Filters?
- What is Middleware?
- What is Routing?
- What is CORS?
- What is Exception Handling?
- What are Status Codes?
- What is Authentication and Authorization?
- JWT Token based Authentication?
- What is Caching?
- What is Distributed Caching and its Importance?
- What is Payload?
- What is Model Binding?
- What is Swagger?
Answers:
1. What is Web API?
- A Web API (Application Programming Interface) is a set of rules and protocols that allows different software systems to communicate with each other over the web. It uses standard HTTP methods like GET, POST, PUT, DELETE to perform operations on resources (data).
Example:
csharppublic class ProductsController : ApiController
{
public IHttpActionResult Get()
{
var products = new List<string> { "Product1", "Product2" };
return Ok(products);
}
}
2. What is REST?
- REST (Representational State Transfer) is an architectural style for designing networked applications. It relies on stateless communication, uses standard HTTP methods, and works with resources that can be identified by URIs.
Example: A RESTful API endpoint for fetching a product:
bashGET /api/products/1
3. Web API vs WCF?
- Web API: Uses HTTP for communication, supports multiple formats like JSON and XML, and is used for lightweight, stateless communication.
- WCF: Supports various protocols (SOAP, HTTP, TCP, etc.), heavier, stateful, and supports complex enterprise-level operations.
Example:
- Web API:csharp
public IHttpActionResult Get() { return Ok(); }
- WCF:csharp
[OperationContract] public string GetData(int value);
4. Stateless vs Stateful Protocols?
- Stateless: Each request from the client to the server is independent. The server does not retain information about previous requests (e.g., HTTP).
- Stateful: The server retains the state between requests (e.g., FTP, TCP).
Example: HTTP (stateless) request:
bashGET /api/products/1
5. What are HTTP Verbs?
- HTTP verbs (also called methods) define the type of action to be performed on a resource:
- GET: Retrieve data.
- POST: Create data.
- PUT: Update data.
- DELETE: Remove data.
Example:
- GET request to retrieve a product:bash
GET /api/products/1
6. GET vs POST?
- GET: Retrieves data from the server. It is idempotent and does not alter the state of the server.
- POST: Sends data to the server to create or update a resource.
Example:
- GET:csharp
public IHttpActionResult GetProduct(int id) { return Ok(product); }
- POST:csharp
public IHttpActionResult PostProduct(Product product) { return CreatedAtRoute("DefaultApi", new { id = product.Id }, product); }
7. PUT vs PATCH?
- PUT: Updates or creates a resource. It is used to update a complete resource.
- PATCH: Applies partial updates to a resource.
Example:
- PUT:csharp
public IHttpActionResult PutProduct(int id, Product product) { }
- PATCH:csharp
public IHttpActionResult PatchProduct(int id, Product product) { }
8. What is the Base class of Api Controller?
- The base class for all Web API controllers is
ApiController
.
Example:
csharppublic class ProductsController : ApiController { }
9. What is API Endpoints?
- An API endpoint is a specific URL where an API can access the resources it needs to perform an operation.
Example: Endpoint to get a product:
bashGET /api/products/1
10. What is API Versioning?
- API Versioning allows you to manage different versions of your API, ensuring backward compatibility when changes are made.
Example:
csharp[Route("api/v1/products")]
public IHttpActionResult GetV1Products() { }
11. What is Minimal API?
- Minimal API in .NET Core allows you to create lightweight APIs with minimal code. It’s designed for simple APIs with minimal overhead.
Example:
csharpvar app = builder.Build();
app.MapGet("/products", () => new { Name = "Product1", Price = 100 });
app.Run();
12. What is Content Negotiation?
- Content Negotiation allows the server to choose the best response format based on the client’s request (e.g., JSON or XML).
Example:
If the client sends Accept: application/json
, the response will be in JSON format.
13. What are Return Types in Web API?
- Web API supports several return types including
IHttpActionResult
,HttpResponseMessage
, and POCO (Plain Old CLR Object).
Example:
csharppublic IHttpActionResult GetProduct(int id) { return Ok(product); }
14. What is Dependency Injection?
- Dependency Injection (DI) is a design pattern where dependencies (e.g., services or repositories) are provided to a class rather than the class creating them.
Example:
csharppublic class ProductsController : ApiController
{
private readonly IProductService _productService;
public ProductsController(IProductService productService) { _productService = productService; }
}
15. What are Filters?
- Filters are used to add additional functionality to Web API methods, such as authentication, logging, exception handling, etc.
Example:
csharp[Authorize]
public IHttpActionResult GetProduct(int id) { return Ok(product); }
16. What is Middleware?
- Middleware is software that provides common services and capabilities to applications. It can handle HTTP requests and responses.
Example: Logging middleware that logs every request:
csharpapp.Use(async (context, next) =>
{
Console.WriteLine("Request received");
await next.Invoke();
});
17. What is Routing?
- Routing determines how HTTP requests are directed to controllers and actions in a Web API application.
Example:
csharpconfig.MapHttpAttributeRoutes();
18. What is CORS?
- CORS (Cross-Origin Resource Sharing) is a mechanism that allows servers to specify who can access their resources.
Example:
csharpconfig.EnableCors(new EnableCorsAttribute("http://example.com", "*", "*"));
19. What is Exception Handling?
- Exception Handling allows you to catch and handle errors during API execution.
Example:
csharptry { /* code */ }
catch (Exception ex) { return InternalServerError(ex); }
20. What are Status Codes?
- HTTP Status Codes are used to indicate the outcome of an HTTP request.
Example:
- 200 OK: The request was successful.
- 404 Not Found: The requested resource could not be found.
21. What is Authentication and Authorization?
- Authentication verifies the identity of a user, while Authorization determines what the user can do after authentication.
Example:
- Authentication: JWT token-based
- Authorization: Role-based access control
22. JWT Token-based Authentication?
- JWT (JSON Web Token) is a compact, URL-safe token format used for authentication. It contains a payload that is encoded and signed.
Example:
csharpvar token = new JwtSecurityTokenHandler().WriteToken(jwtToken);
23. What is Caching?
- Caching stores frequently accessed data to reduce latency and improve performance.
Example:
csharp[OutputCache(Duration = 3600)]
public IHttpActionResult GetProduct() { return Ok(product); }
24. What is Distributed Caching and its Importance?
- Distributed Caching allows caching across multiple servers. It is important for scaling applications in a distributed environment.
Example: Using Redis as a distributed cache.
25. What is Payload?
- Payload refers to the data sent in an HTTP request or response body.
Example: Sending JSON payload in a POST request:
json{ "name": "Product1", "price": 100 }
26. What is Model Binding?
- Model Binding maps HTTP request data to action method parameters.
Example:
csharppublic IHttpActionResult PostProduct(Product product) { return Ok(product); }
27. What is Swagger?
- Swagger is a tool for generating API documentation, testing APIs, and providing a user interface for interacting with your API.
- Get link
- X
- Other Apps
Comments
Post a Comment