初试Web API
2020-12-13 02:29
标签:c style class blog a http Web API 1: 需要环境(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: 需要环境(VS2012or2013/.Net4.5/MVC5) http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api 在一步步跟着实例编写代码的过程中所遇到的问题和需要注意的地方: 1.Web API
2需通过Nuget更新Microsoft.AspNet.WebApi,否则IHttpActionResult等新增的接口无法识别。 PM> Install-Package Microsoft.AspNet.WebApi即可。 2.不同浏览器默认返回的数据格式不定。 IE 和 FF 由于发送了不同的Request Header的 Accept 头,所以 web API 在响应时就发送了不同的内容类型。 比如直接访问http://localhost:8382/api/products/1 (1)火狐返回的是xml accept头:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 (2)IE返回的是json,弹出【文件下载】对话框。 accept头: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,
*/* 如果需要统一Web API 只返回Json,
可以参考: (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.在使用Fiddler 时,IE下无法识别localhost 需通过http://localhost.:8382/api/products 来访问,即localhost 后面加上一个「小数点」。 初试Web API,搜素材,soscw.com 初试Web API 标签:c style class blog a http 原文地址:http://www.cnblogs.com/Benoly/p/3770386.html
{"Id":1,"Name":"Tomato Soup","Category":"Groceries","Price":1.0}