Asp.net-web-api
[Pages:41]-web-api
#web-api
Table of Contents
About
1
Chapter 1: Getting started with -web-api
2
Remarks
2
Examples
2
Installation or Setup
2
What and Why Web API ?
2
To add Web API to an existing MVC application.
2
Chapter 2: Web API Content Negotiation
4
Examples
4
Web API Content Negotiation Basic Information
4
Content Negotiation in Web API
5
Understanding the concept
5
A practical example
6
How to configure in Web API
6
Chapter 3: WEB API CORS Enabling
8
Examples
8
Enabling CORS for WebAPI 2
8
Enabling CORS globally for Web API 2
8
Enabling CORS in 5 for all domains and methods
8
Enabling CORS in 5 for specific domains and methods
8
Configure CORS for WebAPI 2 with Windows Authentication
9
Properly send authenticated request from jQuery against Web API 2 endpoint
10
Properly send authenticated request from AngularJS against Web API 2 endpoint
11
Chapter 4: Web API MediaTypeFormatter
13
Examples
13
MediaTypeFormatter Basic Information
13
Chapter 5: Attribute Routing in WebAPI
16
Introduction
16
Syntax
16
Parameters
16
Remarks
16
Examples
16
Basic Attribute Routing
16
Route Prefix Attribute
17
Chapter 6: Caching
18
Remarks
18
Examples
18
System.Runtime.Caching (MemoryCache)
18
Chapter 7: Configure a Web API application to respond with pretty/formatted JSON data by d 20
Examples
20
Default JSON formatting: Efficiency at the cost of readability
20
Chapter 8: Creating A Custom ActionFilterAttribute
22
Introduction
22
Examples
22
EnsurePresenseOfAttribute
22
Controller Before EnsuresPresenseOf Attribute
23
Update Controller
23
Chapter 9: OData with Web API
25
Examples
25
Install the OData Packages
25
Enable Entity Framework
25
Configure the OData Endpoint
26
Add the OData Controller
27
Performing CRUD on the Entity Set
27
Querying the Entity Set
27
Adding an Entity to the Entity Set
28
Updating an Entity
28
Deleting an Entity
29
Chapter 10: Quick Start: Working with JSON
31
Remarks
31
Examples
31
Return JSON from GET using attributes
31
1. Setup your formatter and routing in Register of (App_Start/WebApiConfig)
31
2. Create methods in an ApiController
31
Chapter 11: Web API Url Routing
33
Examples
33
How Routing works in webapi
33
Verb based routing examples.
35
Credits
37
About
You can share this PDF with anyone you feel could benefit from it, downloaded the latest version from: asp-net-web-api
It is an unofficial and free -web-api ebook created for educational purposes. All the content is extracted from Stack Overflow Documentation, which is written by many hardworking individuals at Stack Overflow. It is neither affiliated with Stack Overflow nor official -web-api.
The content is released under Creative Commons BY-SA, and the list of contributors to each chapter are provided in the credits section at the end of this book. Images may be copyright of their respective owners unless otherwise specified. All trademarks and registered trademarks are the property of their respective company owners.
Use the content presented in this book at your own risk; it is not guaranteed to be correct nor accurate, please send your feedback and corrections to info@
1
Chapter 1: Getting started with -webapi
Remarks
This section provides an overview of what -web-api is, and why a developer might want to use it. It should also mention any large subjects within -web-api, and link out to the related topics. Since the Documentation for -web-api is new, you may need to create initial versions of those related topics.
Examples
Installation or Setup
Detailed instructions on getting -web-api set up or installed.
What and Why Web API ?
What? : A fully supported and extensible framework for building HTTP based endpoints. In the world of HTML5, mobile devices, and modern development techniques HTTP have become the default option for building rich, scalable services. The Web API provides an easy to use set of default options but also provides a deep extensibility infrastructure to meet the demands of any scenario using HTTP. Why? :
? An HTML5 application that needs a services layer. ? A mobile application that needs a services layer. ? A client-server desktop application that needs a services layer.
To add Web API to an existing MVC application.
Use Nuget to find the Web Api Package. You can do that either by using the Manage Nuget Packages and searching for the Web Api package or use Nuget Package Manager and type
PM> Install-Package Microsoft.AspNet.WebApi
Add WebApiConfig.cs to the App_Start/ folder The config file should contain this.
using System.Web.Http;
2
namespace WebApplication1 { public class WebApiApplication : System.Web.HttpApplication {
protected void Application_Start() {
GlobalConfiguration.Configure(config => {
config.MapHttpAttributeRoutes();
} } }
config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional }
); });
Source : Configuring Web API
Add GlobalConfiguration.Configure(WebApiConfig.Register); in Application_Start of the Global.asax file.
Read Getting started with -web-api online:
3
Chapter 2: Web API Content Negotiation
Examples
Web API Content Negotiation Basic Information
Content Negotiation can be defined as the process of selecting best representation for a given resource. So Content negotiation means the client and server can negotiate between them so that client can get data according to their required format.
There are three points on which internet depends,
? The Resource ? A Pointer to resource(URL) ? Representation of resource
Third point is more important than other two, because everything is works on the basis of how we can see the resource. We can represent a resource in two formats.
1. XML(Extensible Markup Language) Format 2. JSON(JavaScript Object Notation) Format
One of the standards of the RESTful service is that, the client should have the ability to decide in which format they want the response either in JSON or XML. A request that is sent to the server includes an Accept header. Using the Accept header the client can specify the format for the response.
For example,
Accept: application/xml returns result in XML format
Accept: application/json returns result in JSON format
Depending on the Accept header value in the request, the server sends the response. This is called Content Negotiation.
What happens behind the scene when we request data in specific format?
The Web API controller generates the data that we want to send to the client and hands the data to the Web API pipeline which then look for Accept header of the client. Then, choose a appropriate formatter to format the data.
As Web API is greatly extensible, we can also specify multiple values for accept header in the request header.
Accept: application/xml,application/json
4
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.