Search This Blog

Tuesday, December 1, 2020

Q11-Q15

 Q11. How can I restrict my API methods to expose to all? I want to expose client specific api methods. 
Q12. How API asynchronous methods calls happen? what is Task keyword?
Q13. Meaning of various HTTP Status Codes?
Q14. What are the various return types of a controller action in webapi?
Q15. In case of unhandled exception, what would be the default http response code we will return?
=======================================================================
Q11. How can I restrict my API methods to expose to all? I want to expose client specific api methods. 

Answer:
Following way we can do this. 
  1. Using API key techniques. In this we have api key as per each client and API key is cross checked for each client. We can have client ids annotation on web api methods. Here only few clients are allowed. 




2. We can use cloud to restrict clients on api access. we can different methods name based on client. 

3. I can use Authenticate attribute in mvc application and using authentication filter and I can have some logic over there.  

=======================================================================
Q12. How API asynchronous methods calls happen? what is Task keyword?

Answer:
We have few Async programming pattern. 

Task based Asynchronous Pattern- 
  • The Microsoft .NET Framework 4.0 introduces a new Task Parallel Library (TPL) for parallel computing and asynchronous programming. The namespace is "System.Threading.Tasks".
  • A Task can represent an asynchronous operation and a Task provides an abstraction over creating and pooling threads.










C# 5.0 async and await based Asynchronous Pattern

  • Two new keywords, async and await, were introduced in C# 5.0 and .NET 4.5. 
  • These are implemented at the compiler level and built on top of the "System.Threading.Tasks.Task" feature of .NET 4.0.
  1. async void LoadEmployee_Click(object sender, RoutedEventArgs e) {  
  2.     // ...  
  3.     await viewer.LoadEmplployeeAsync();  
  4.     // ...  
  5. }

=======================================================================
Q13. Meaning of various HTTP Status Codes?

Answer:
2xx: Success
It means the action was successfully received, understood, and accepted.
  • 200 OK The request is OK.
  • 201 Created The request is complete, and a new resource is created .
  • 202 Accepted The request is accepted for processing, but the processing is not complete.
  • 204 No Content A status code and a header are given in the response, but there is no entity-body in the reply.
4xx: Client Error
It means the request contains incorrect syntax or cannot be fulfilled.
Check webApi #15

5xx: Server Error
It means the server failed to fulfill an apparently valid request
Check webApi #15

=======================================================================
Q14. What are the various return types of a controller action in webapi?

Answer:
The Web API action method can have following return types.
  1. Void
  2. Primitive Type/Complex Type - retun types like int, string, list<employees> etc
  3. HttpResponseMessage - HttpResponseMessage is used when we want to customize the return type (action result) of an action method. Responses are customized by providing status code, content type, and data to be returned to HttpResponseMessage.
  4. IHttpActionResult - The IHttpActionResult interface was introduced in Web API 2. Essentially, it defines an HttpResponseMessage factory.
Knowledge bomb
IHttpActionResult is a way for creating responses introduced in WebAPI2 but IActionResult is more leaned towards ASP.NET MVC for returning the result of an action method. Also IActionResult is widely used in .net Core as well.

Q15. In case of unhandled exception, what would be the default http response code we will return in webapi?

Answer:
In case of unhandled exception we will return Http: 500 Internal server error in case of error. 

  1. 400 -  Bad Request - This is the generic error that tells us someone created a bad request. Perhaps required fields are missing or header values are not filled in.
  2. 401- Unauthorized Indicates that authentication has failed. This could be because of an expired, missing, or invalid token.
  3. 403 - Forbidden - Indicates that authorization failed. Alternatively, you can also use 404 so that the consumer doesn’t know that the resource even exists.
  4. 404 - Not Found - The requested resource is not found. Companies like GitHub also use 404 if you try to access a resource that you do not have the authorization to access.
  5. 500 - Internal Server Error - When something goes wrong on the server, the consumer can’t do anything about it. Just let them know there’s a problem and that they should try again later or contact support.

No comments:

Post a Comment