Preliminary web API

Source: Internet
Author: User

Web API 1:

Required Environment (VS2010/.NET4.0/MVC4)

Http://www.asp.net/web-api/overview/creating-web-apis/creating-a-web-api-that-supports-crud-operations

Web API 2:

Required Environment (VS2012OR2013/.NET4.5/MVC5)

Http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

The problems and areas to be aware of in the process of writing code with an instance in one step:

1.Web API 2 needs to update MICROSOFT.ASPNET.WEBAPI through NuGet, otherwise the new interfaces such as Ihttpactionresult are not recognized.

Pm> install-package Microsoft.AspNet.WebApi can be.

2. The data formats returned by default in different browsers are variable.

IE and FF send different request headers for the Accept header, so the Web API responds with a different content type.

such as direct access to HTTP://LOCALHOST:8382/API/PRODUCTS/1

(1) Firefox returns XML

<product><category>groceries</category><id>1</id><name>tomato Soup</Name ><Price>1</Price></Product>

Accept Header: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

(2) IE returns the JSON, which pops up the "File Download" dialog box.

{"Id": 1, "Name": "Tomato Soup", "Category": "Groceries", "Price": 1.0}

Accept header: Image/gif, Image/jpeg, Image/pjpeg, Image/pjpeg, Application/x-shockwave-flash, application/ X-ms-application, APPLICATION/X-MS-XBAP, Application/vnd.ms-xpsdocument, Application/xaml+xml, application/ Vnd.ms-excel, Application/vnd.ms-powerpoint, Application/msword, */*

If you need the unified Web API to return only JSON, you can refer to:

(1) http://www.cnblogs.com/acles/archive/2013/06/21/3147667.html

(2) http://www.strathweb.com/2013/06/supporting-only-json-in-asp-net-web-api-the-right-way/

(3) http://www.strathweb.com/2012/07/everything-you-want-to-know-about-asp-net-web-api-content-negotation/

3. LocalHost is not recognized under IE when using fiddler

It needs to be accessed via http://localhost.:8382/api/products, which is followed by a "decimal point" behind localhost.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.