官术网_书友最值得收藏!

Introducing web API

Any method(s) that is accessible over the web using HTTP verbs is called a web API. It is a lightweight way of transmitting data over HTTP, easily consumed by various clients like browsers, desktop apps, handheld devices, or even other SOA applications.

For a web API to be a successful HTTP-based service, it needed a strong web infrastructure like hosting, caching, concurrency, logging, security, and so on. One of the best web infrastructures was none other than ASP.NET.

ASP.NET, either in the form of a Web Form or an MVC, was widely adopted, so the solid base for web infrastructure was mature enough to be extended as a Web API.

Microsoft responded to the community needs by creating ASP.NET Web API--a super-simple yet very powerful framework for building HTTP-only, JSON-by-default web services without all the fuss of WCF.

The ASP.NET Web API can be used to build REST-based services in a matter of minutes, and can be easily consumed with any of the frontend technologies.

It was launched in 2012 with the most basic needs for HTTP-based services like convention-based Routing, HTTP Request, and Response messages.

Later, Microsoft released the much bigger and better ASP.NET Web API 2 along with ASP.NET MVC 5 in Visual Studio 2013.

ASP.NET Web API 2 evolved at a much faster pace with these features:

  • Installing of the web API 2 was made simpler by using NuGet; you can create either an empty ASP.NET or MVC project, and then run the following command on the NuGet Package Manager Console:
        Install-Package Microsoft.AspNet.WebApi
  • The initial release of the web API was based on convention-based routing, which means that we define one or more route templates, and work around it. It's simple without much fuss, as the routing logic is in a single place, and it's applied across all controllers.
  • The real-world applications are more complicated with resources (controllers/ actions) having child resources, for example, customers having orders, books having authors, and so on. In such cases, convention-based routing is not scalable.
  • Web API 2 introduced a new concept of Attribute Routing, which uses attributes in programming languages to define routes. One straightforward advantage is that the developer has full control over how URIs for the web API are formed.
  • Here a is quick snippet of Attribute Routing:
        Route("customers/{customerId}/orders")] 
        public IEnumerable<Order>GetOrdersByCustomer(int customerId) { ... } 
For more details on this, read Attribute Routing in ASP.NET Web API 2 at https://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2.
  • An ASP.NET Web API lives on the ASP.NET framework, which may lead you to think that it can be hosted on IIS only. However, using OWIN self-host, it can be hosted without IIS also.
  • If any web API is developed using either the .NET or non-.NET technologies, and is meant to be used across different web frameworks, then enabling CORS is a must.
A must read on CORS and ASP.NET Web API 2 can be found at this link: https://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api.
  • IHTTPActionResult and web API OData improvements are other few notable features which helped web API 2 evolve as a strong technology for developing HTTP-based services.
  • ASP.NET Web API 2 has become more powerful over the years with C# language improvements like asynchronous programming using Async/Await, LINQ, Entity Framework Integration, Dependency Injection with DI frameworks, and so on.
主站蜘蛛池模板: 屏南县| 滦平县| 常州市| 漾濞| 霍林郭勒市| 松江区| 垫江县| 湘潭县| 当阳市| 蓬溪县| 汉源县| 阆中市| 如东县| 阳江市| 邯郸县| 饶平县| 五家渠市| 海阳市| 嘉禾县| 三穗县| 太康县| 通河县| 南岸区| 上思县| 铁力市| 玉门市| 石阡县| 格尔木市| 天台县| 霍邱县| 无棣县| 洛南县| 澄江县| 西丰县| 休宁县| 房产| 庄浪县| 巫山县| 许昌市| 丹江口市| 绍兴县|