Getting Started With ASP.Net Web API 2 : Day 1

Introduction

Service technology is a new trend in the World Wide Web, where Microsoft provides the modularity to use and easy to make the service with the .NET framework. Some years ago, when we were using the service with XML as a web Service (.ASMX) in 2002 and then Microsoft added the broadly capable Windows Communication Foundation (WCF) a few years later.

Microsoft gave .NET developers many options for building SOAP-Based services. With some basic configuration changes, we could support a wide array of communication protocols, authentication schema, message format, as the WCF standard. In short, now a days the demand for mobile-to--service communication and browser-based, single-page applications have evolved in the market rapidly. It was no longer enterprise services talking SOAP/RPC to each other. These application needed a simple HTTP-only, JSON-compitable back end. The internet needs of these applications look like a connected version,  REST.

The ASP.NET Web API is a powerful framework for building HTTP-only, JSON-by-default web service without all the fuss (no setting of endpoints, neither setting of contracts) of WCF. Model binding works out-of-the-box and returns the plain-old CLR Objects. From these frameworks we can make RESTful services in a few minutes.   

Advantages of Web API

AdvantageOfWebAPi

  • Configuration

    WCF has endpoints and contracts. There are no settings for those things in the Web API.
     
  • Rest by Default

    URL Routing features provided by the framework are unlike a WCF where a service is an address to a physical file. (In other words, an address that maps directly to a services class or .SVC file.) A service address with the Web API is a restful route that maps to a controller method.
     
  • Simpler Extensible Processing Pipeline

    The Web API provides a highly-extensible and much simpler processing pipeline. For example, delegating handlers and filters are mechanisms providing pre-processing and post-processing capabilities.

    Handlers allow us to execute custom code prior to any controller being activated within the application. In fact, handlers can be configured to handle routes that have no corresponding controller.

    Filters are very useful classes that contain a few methods allowing to run some code before and after specific controller methods are invoked. These are action filters, authorization filters and exception filters. These filters take the form of attributes and they are either decorated on specific controller methods, decorated on the controllers themselves or configured globally for all methods.
     
  • Abstraction with Routes

    Routes provide the Web API service developer a layer of abstraction between what the users see and the underlying implementation. We can map any URL to any controller method. When the API signature (in other words, the REST URL) isn't hard-wired to a specific interface, class, or .svc file, we are free to update our implementation of that API method, as long as the URL specification for that method remains valid.
     

Features of the ASP.NET Web API

FeaturesOfWebApi

  • Convention-based CRUD Actions

    HTTP actions (for example, GET and POST) are automatically mapped to controller methods (also known as controller actions). If a controller called Products does a GET request such as /api/products then it will automatically invoke a method named “Get” on the controller. Then the Web API automatically matches the number of arguments given in the URL to an appropriate controller method. Therefore, the URL /api/products/2 would automatically invoke the GetById(long id) method. The same magic also applies to POST, PUT, and DELETE calls.
     
  • Built-in Content Negotiation

    In MVC, controller methods that return JSON or XML are hard-coded to specifically return one of those content types. But with the Web API, the controller method need only return the raw data value, and this value will be automatically converted to JSON or XML, per the caller's request.
     
  • Attribute Routing and Route Prefixes

    RoutePrefix, and various Http* attributes (for example, HttpGet, HttpPost) to explicitly define routes and associated HTTP actions for our controllers.
     
  • Route Constraints

    This very cool feature provides a mechanism for constraining various controller methods and their routes to specific business rules. For example, rather than just {id} in your route, we can now include something like:
    {id:int},
    {id:min(10)},
    {id:range(1,100)},
    {phone:regex(^\d{3}-\d{3}-\d{4}$)}.
     
  •  CORS Support

    The new EnableCors attribute allows us for an API to allow cross-origin requests from JavaScript applications.
     
  • Global Error Handling

    In this feature all the unhandled exceptions can now be caught and handled through one central mechanism. The framework now supports multiple exception loggers that have access to the actual unhandled exception and the context in which it occurred.

Summary

In this article, we learned how the ASP.NET Web API provides a great platform for building REST-style Web APIs. Where much of the power and flexibility of WCF and SOAP aren't needed, Web API can be a very simple and elegant alternative. Where applications that need to support only HTTP communication, as well as those that focus heavily on text-formatted messages.

Go to next part>>


Similar Articles