入门:添加一个支持获取单一资源以及支持POST_PUT和DELETE方法

前端之家收集整理的这篇文章主要介绍了入门:添加一个支持获取单一资源以及支持POST_PUT和DELETE方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

原文地址


WCF Web API支持多个宿主环境:自宿主(windows服务或者控制台)和IIS宿主(asp.net webform/mvc)。这个入门文章主要演示在ASP.NET MVC3网站宿主,主要演示如何在一个Web API上允许更新:

如何检索一个特定项的资源
如何在API上启用HTTP POST, PUT和DELETE方法
如何通过HTML表单发送一个POST到API
这篇入门文章的场景是允许客户端添加删除和更新系统的联系人。

1、解压启动器代码

快速开始练习,请到这里下载代码,下载解压后,打开Start目录下的ContactManager项目。代码和入门:构建简单的Web API的主要区别是实体代码重构到一个内存 ContactManagerRepository。

@H_502_18@public interface IRepository<T> { T Find(int id); IQueryable<T> FindAll(); void Add(T entity); Removeint id); Save(); } using System; using System.Collections.Generic; using System.Linq; using ContactManager.Repositories; namespace ContactManager.Infrastructure { public abstract class InMemoryRepository<T> : IRepository<T> where T:new() { protected List<T> entities; protected int nextId; static InMemoryRepository<T> instance; static object lockObject = new object(); public T int id) { return entities.SingleOrDefault(e => IsEntityWithId(e,id)); } public IQueryable<T> FindAll() { return entities.AsQueryable(); } public (T entity) { OnAdd(entity,nextId++); entities.Add(entity); } int id) { entities.RemoveAll(e => IsEntityWithId(e,id)); } () { throw new InvalidOperationException(); } protected abstract bool IsEntityWithId(T contact,int id); OnAdd(T entity,239)">int newId); } } using ContactManager.Infrastructure; using ContactManager.Resources; namespace ContactManager.Repositories { public interface IContactRepository : IRepository<Contact> { } }

2、启用检索一个单一的资源并和HttpResponseException协同工作

目前我们的API只支持获取一个联系人集合。另一个通常的场景是通过一个URI返回一个单一的资源,如果找不到相关的资源应该返回一个404状态码。

打开ContactsAp.cs
复制以下方法

  1. [WebGet(UriTemplate="{id}")]
  2.  
  3. public Contact GetItem(int id)
  4.  
  5. {
  6.  
  7. var contact = repository.Find(id);
  8.  
  9. if (contact == null)
  10.  
  11. throw new HttpResponseException(HttpStatusCode.NotFound);
  12.  
  13. return contact;
  14.  
  15. }

注意GET方法接受一个ID参数映射到{id} uri模板参数。如果你的请求API是http://localhost:9000/api/contacts/1 的ID将被设置为1,Web API支持将模板参数自动转换为原生类型int。
如果联系人不存在,就抛出HttpResponseException 并设置状态码
编译并运行(F5)
打开Fiddler并在“Request builder”栏输入地址“http://localhost:9000/api/contacts/1”
拷贝以下内容到header
Accept: application/json

运行执行按钮,Contract 1按json格式返回
HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Tue,19 Jul 2011 13:04:26 GMT
X-AspNet-Version: 4.0.30319
Content-Length: 35
Cache-Control: private
Content-Type: application/json; charset=utf-8
Connection: Close

{"ContactId":1,"Name":"Phil Haack"}

3、添加对POST的支持

以下代码添加一个新的Post方法添加一个新的Contract

  1. WebInvoke(UriTemplate = "",Method="POST")]
  2.  
  3. public Contact Post(Contact contact)
  4.  
  5. {
  6.  
  7. repository.Add(contact) 上面代码里用到了WebInvokeAttribute,对于所有的HTTP GET以外的其他方法,使用此属性 方法指定的参数的必须是大写的。
    4、以Json格式发送数据
    Web Api允许以多个格式发送内容,下面是使用fiddler发送jsonPOST
    运行项目
    启动Fiddler并切换到“Request Builder
    选择“POST方法,输入以下Urihttp://localhost:9000/api/contacts
    拷贝以下内容到“Request Headers

  8. Accept: application/json
  9. Content-Type: application/json 拷贝以下内容到“Request Body {"Name":"New Person1"} image 按下“Execute”,返回Json格式的新的Contact id7 HTTP/1.1 200 OK Server: ASP.NET Development Server/10.0.0.0 Date: Tue,19 Jul 2011 13:12:57 GMT X-AspNet-Version: 4.0.30319 Content-Length: 36 Cache-Control: private Content-Type: application/json; charset=utf-8 Connection: Close {"ContactId":7,"New Person1"} 5、以XML格式发送数据 xml方式发布,需要替换“Request Headers”为以下内容 Content-Type: application/xml Accept: application/替换“Request Body”为以下内容 <Contact> <Name>New Person2</Name> </Contact> 
  10. 按下“Execute”,然后双击左窗格中的结果,选择“RAW标签,返回的结果应该是XML,并显示创建了ID8的一个联系人

  11.  
  12.  
  13. HTTP/1.1 200 OK
  14. Server: ASP.NET Development Server/10.0.0.0
  15. X-AspNet-Version: 4.0.30319
  16. Content-Length: 105
  17. Cache-Control: private
  18. Content-Type: application/xml; charset=utf-8
  19. Connection: Close
  20. @H_580_301@<?xml version="1.0" encoding="utf-8"@H_580_301@?><Contact><ContactId>8</ContactId><Name>New Person2</Name></Contact> 

  21. 原文地址

猜你在找的设计模式相关文章