• 1. What is a web API?

    Answer:

    A Web API stands for application programming interface. It's either a web server or a web browser. It is a web development concept, usually limited to a web application's client-side and building HTTP-based services that can be accessed in different applications on different platforms such as web, windows, mobile, etc. It is like a web service or WCF service but the exception is that it only supports HTTP protocol.

  • 2. Which protocol does the Web API support?

    Answer:

    It’s a WEB API so it only supports HTTP protocol.

  • 3. When should I choose a Web API?

    Answer:

    Web API in asp.net core helps to share one part or everything related to business logic code, database, service for cross-platform can be used together. So you can choose Web API when:

    • You want to create resource-oriented services over HTTP that can use the full features of HTTP (like URIs, request/response headers, caching, versioning, various content formats).
    • You want to expose your service to a broad range of clients including browsers, mobiles, iPhones and tablets.
    • You have a big project, you want to share some modules in that project or sell it to clients. You can make a web API to share it.

    For some example:

    - When you have some JS projects such as Vuejs, React or Angular you’ll need one or more web APIs to provide data for those projects.

    - When you have a mobile app(app installed on Android, IOS) you can create a web API to provide data or logic code for that app.

    - I just built a project to help translate the language. It is the same with Google translate, and now I want to sell it to anyone who wants to use this service. I can make a web API service and sell it.
     

  • 4. What are the advantages of the Web API?

    Answer:
    • Centralization of business logic will lessen your efforts, making your work worthwhile. As a result, business information can be maintained with consistency.
    • These RESTful Web APIs are accessible by a variety of HTTP clients like Windows 7, Windows 8, Android, iPhone, iPad, WPF Client, Windows Phone 8, etc...
    • It is open-source.
    • It uses low bandwidth. (XML or JSON data). We can also pass HTML content if required.
    • The Web API Controller pattern is much similar to MVC Controller. Thus, it becomes easy to maintain and understand.
    • Web API is not part of the MVC Framework but it is part of Asp.net Framework. Therefore, no other dependencies are there for development and even deployment.
    • Easy to test.
    • No required contract.
  • 5. What is the difference between Web API and WCF in C#?

    Answer:
    • WCF offers request-reply, one-way, or duplex while Web API is by default request-reply only.
    • WCF is used for developing SOAP-based services whereas Web API is used for both SOAP-based and RESTful services.
    • WCF does not offer any support for MVC features whereas Web API supports MVC features.
    • WCF supports HTTP, UDP, and custom transport protocol whereas Web API supports only HTTP protocol.
    • WCF offers Text, MTOM, and Binary Encoding support whereas Web API supports the UTF-8 encoding format.
    • WCF supports message queues, message security, duplex communication, transaction whereas Web API doesn’t support.
    • WCF stands for Windows Communication Foundation whereas API stands for Application Program Interface.
  • 6. What is the RESTFUL API?

    Answer:

    A RESTful API is based on representational state transfer (REST), which is an architectural style and approach to communications often used in web services development. It’s also an architectural style for an application program interface (API) that uses HTTP requests to access and use data. That data can be used to GET, PUT, POST and DELETE data types, which refers to the reading, updating, creating and deleting of operations concerning resources. It's a way to help manage resources very useful.

  • 7. What is routing and how to map it in Asp.Net Core Web API?

    Answer:

    The Router in Asp.Net Core API is the same as MVC. It helps rewrite the URL default and make it feel easy to understand and remember.

    To use the Router in Web Api .Net Core, we can use the action filter attribute Route to help rewrite the URL. For an example:

    [ApiController]
    [Route("api/authetication")]
    public class AuthenticationController : ControllerBase
    {
        [HttpPost]
        [Route("login")]
        public IActionResult Login([FromBody]RefreshTokeRequest model)
        {
            // Other code here
        }
    }

    You can access with URL /api/authetication/login

  • 8. How many methods are supported in the web API?

    Answer:

    The four main HTTP methods (GET, PUT, POST, and DELETE) can be mapped to CRUD operations as follows:

    • GET retrieves the representation of the resource at a specified URI. GET should have no side effects on the server.
    • PUT updates a resource at a specified URI. PUT can also be used to create a new resource at a specified URI if the server allows clients to specify new URIs. For this tutorial, the API will not support creation through PUT.
    • POST creates a new resource. The server assigns the URI for the new object and returns this URI as part of the response message.
    • DELETE deletes a resource at a specified URI.
    • PATCH it's the same with PUT but it is used when you only want to update one or some fields into the database. For example, change the status of an article, activate/ban a member...
       
  • 9. What is the difference between GET and POST methods in the web API?

    Answer:

    Here are the major differences between GET and POST:

    GET POST 
     Use for retrieve data Use for submitting and collect information from users.
     Can be cache by URL Can’t cache
     Parameters are visible in the URL. parameters are not visible in the URL.
     GET has a limitation on the length of the values, generally 255 characters No limitation on the length of the values since they are submitted via the body of HTTP.
     Supports only string data types.  Supports different data types, such as string, numeric, binary, image, etc.
     Can be bookmarked Cannot be bookmarked
     GET requests should never be used when dealing with sensitive data You can submit sensitive data, ex: password...
  • 10. What is the difference between PUT and PATCH methods in the web API?

    Answer:

    PUT and PATCH methods are used for updated information. But the main difference between them is:

    PUT

    In HTTP.PUT method the resource is first identified from the URL and if it exists then it is updated otherwise a new resource is created. When the target resource exists it overwrites that resource with a completely new body. That is HTTP.PUT method is used to CREATE or UPDATE a resource.
    The HTTP put method is like a MERGE query in SQL which inserts or updates a record depending upon whether the given record exists.

    PUT request is idempotent i.e. hitting the same requests twice would update the existing recording (No new record created). In the PUT method, the resource id is decided by the client and provided in the request URL.

    Example: Use the PUT method to update existing users or orders.

    PATCH

    An HTTP.PATCH method is used for partial modifications to a resource i.e. delta updates.

    The HTTP patch method is like an UPDATE query in SQL which sets or updates selected columns only and not the whole row.

    Example: You could use the PATCH method to update order status.

  • 11. Can I return a view from the web API Asp.Net Core?

    Answer:

    No, Web Api in Asp.Net Core does not allow return an HTML view but you can return Html tag through a variable or an object with one property.

  • 12. What is the difference between Web API and MVC in Asp.net Core?

    Answer:
    • Asp.Net MVC is used to create web applications that return both views and data but Asp.Net Web API is used to create full-blown HTTP services in an easy and simple way that returns only data, not view.
    • Web API helps provide services, data for cross-platform, Web Mvc can call a Web API to retrieve data or anything.
    • MVC controller has too many features like its return views, action result, javascript result, etc but in Web API has either JSON or XML.
       
  • 13. How to handle exceptions in Asp.Net Core Web API?

    Answer:

    We have many ways to handle exceptions in the Asp.net Core Web API. We can use middleware or custom action filter attributes to catch global exceptions. You also use a try-catch block to handle exceptions in each method.

RATE