April 25, 2016

ASP.NET Web API

ASP.NET Web API Versions:
Web API Version
Supported .NET Framework
Coincides with
Web API 1.0
.NET Framework 4.0
ASP.NET MVC 4
Web API 2
.NET Framework 4.5
ASP.NET MVC 5

ASP.NET Web API Characteristics:
1.       ASP.NET Web API is an ideal platform for building RESTful services.
2.       ASP.NET Web API is built on top of ASP.NET and supports ASP.NET request/response pipeline
3.       ASP.NET Web API maps HTTP verbs to method names.
4.       ASP.NET Web API supports different formats of response data. Built-in support for JSON, XML, BSON format.
5.       ASP.NET Web API can be hosted in IIS, Self-hosted or other web server that supports .NET 4.0+.
6.       ASP.NET Web API framework includes new HttpClient to communicate with Web API server. HttpClient can be used in ASP.MVC server side, Windows Form application, Console application or other apps.
  • What is Web API?  API is some kind of interface which has a set of functions that allow programmers to access specific features or data of an application, operating system or other services.
  • Create Web API Project
    • Web API with MVC Project
    • Stand-alone Web API Project
  • Test Web API – Fidder, postman
1.       It must be derived from System.Web.Http.ApiController class.
2.       It can be created under any folder in the project's root folder. However, it is recommended to create controller classes in the Controllers folder as per the convention.
3.       Action method name can be the same as HTTP verb name or it can start with HTTP verb with any suffix (case in-sensitive) or you can apply Http verb attributes to method.
4.       Return type of an action method can be any primitive or complex type.
  • Configure Web API  
  • Global.asax
    • GlobalConfiguration.Configure(WebApiConfig.Register)
  • WebApiConfig
public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }

1.       Convention-based Routing
2.       Attribute Routing
1.       FromUri
2.       FromBody 
1.       Void
2.       Primitive type or Complex type
3.       HttpResponseMessage
4.       IHttpActionResult
1.       Media type specifies the format of the data as type/subtype
2.       Content-Type header attribute (Body)
3.       Accept Header attribute
Media Type Formatter Class
MIME Type
Description
JsonMediaTypeFormatter
application/json, text/json
Handles JSON format
XmlMediaTypeFormatter
application/xml, text/json
Handles XML format
FormUrlEncodedMediaTypeFormatter
application/x-www-form-urlencoded
Handles HTML form URL-encoded data
JQueryMvcFormUrlEncodedFormatter
application/x-www-form-urlencoded
Handles model-bound HTML form
URL-encoded data


Filter Type
Interface
Class
Description
Simple Filter
IFilter
-
Defines the methods that are used in a filter
Action Filter
IActionFilter
ActionFilterAttribute
Used to add extra logic before or after action methods execute.
Authentication Filter
IAuthenticationFilter
-
Used to force users or clients to be authenticated before action methods execute.
Authorization Filter
IAuthorizationFilter
AuthorizationFilterAttribute
Used to restrict access to action methods to specific users or groups.
Exception Filter
IExceptionFilter
ExceptionFilterAttribute
Used to handle all unhandled exception in Web API.
Override Filter
IOverrideFilter
-
Used to customize the behaviour of other filter for individual action method.


No comments: