Saturday, May 6, 2023

Web API Versioning.

 Implement the new feature without impacting the existing consumers we can solve this problem by API versioning.
When the business has started to grow and expand, new requirements arise. Due to this, we may need to provide more functionality in the existing APIs. However, existing Web API can be consumed by many clients so how to implement the new feature without impacting the existing consumers? We can solve this problem by versioning our API.

Different ways of implementing Versioning in Web API.
1.Query String
2. URL.
3. HTTP Header
There are other ways as well, like accept-header and content type.

Wednesday, April 12, 2023

How to improve performance of the Web API

1. Use Parallel Programming in Web API 

2. Compress the Result of Web API

By compressing the API Response, we have the following two advantages.
  • Data size will be reduced
  • Response time will increase (increasing the speed of the communication between the Client and Server.)
Using DotNetZIP.

2. 

Using Caching to Improve Performance


Monday, April 10, 2023

Otput caching in MVC/API

 Caching is used to improve the performance in ASP.NET MVC. Caching is a technique which stores something in memory that is being used frequently to provide better performance. In ASP.NET MVC, OutputCache attribute is used for applying Caching. OutputCheching will store the output of a Controller in memory and if any other request comes for the same, it will return it from cache result.


OutputCache attribute can have a parameter.

  1. [OutputCache(Duration = 60)]  
  2. public ActionResult Index() {  
  3.     var emps = from e in db.Employees  
  4.     orderby e.ID  
  5.     select e;  
  6.     return View(emps);  

VaryByPara

It describes cache will be stored on the basis of a parameter. Cache will be stored on the basis of the list of semicolon separated by a string.

The example of VaryByParam is given below.

https://www.c-sharpcorner.com/article/how-to-implement-caching-in-the-net-core-web-api-application/
  1. [OutputCache(Duration = 60, VaryByParam = "Id")]  
  2. public ActionResult Index(int Id) {  
  3.     var emps = from e in db.Employees where e.DeptID = Id  
  4.     orderby e.ID  
  5.     select e;  
  6.     return View(emps); 

What is difference between Rest and Restful API

 The REST API follows all the rules of the REST Architecture. It has a client-server, stateless, cacheable, layer system with a uniform interface, whereas the RESTful web applications have all the features of the REST architecture with unique additional features. The REST API has a separate system to handle application information.




Sunday, April 9, 2023

What is the purpose of webHostBuilder()

 The webHostBuilder() as the name suggests is a factory used to create a web host for a web application. This function also configures the bits needed by the web host to run the application. It is a part of Microsoft.AspNet.Hosting namespace.

What is content negotiation in ASP.Net Web API?

 Content negotiation is basically a process of selecting the best representation from multiple representations that are available for a given response.

 It simply allows one to choose rather than negotiate content that one wants to get in response. It is performed at the server-side. In simple words, 

it chooses the best media type for matters to return a response to an incoming request.


There are two types of headers available for content negotiation:

  • Content-Type: This header tells the server about the information that it will receive from the client.
  • Accept: This header shows the data format requested by the client from the server.
We know that there are three pillars of the internet, they are:
  • The resource
  • The URL
  • The representation

The formal definition of Content Negotiation is “the process of selecting the best representation for a given response when there are multiple representations available”.

By checking the “Accept” header, the Web API understands which representation the client is able to accept. For example, if we specify that the client can understand the following representation:
 
application/xml , application/json, text/javascript



What are the main return types supported in ASP. Net Web API?

 It supports the following return types:

HttpResponseMessage

IHttpActionResult

Void

Other types such as string, int, etc

Web API Versioning.

 Implement the new feature without impacting the existing consumers we can solve this problem by API versioning. When the business has start...