以Web Host的方式来寄宿Web API

一、新建一个Common的类库项目并新建一个测试用的Contact实体类

 namespace Common
{
public class Contact
{
public string Id { get; set; }
public string Name { get; set; }
public string PhoneNum { get; set; }
public string EmailAddress { get; set; }
public string Address { get; set; }
}
}

二、新建一个WebApi的类库项目并新建一个测试用的ContactsController类

表现为HttpController 的Web AP1定义在WebApi项目之中,我们一般将ApiController作为继承的基类。ApiControIIcr定义在
“system.Web Httpd1” 程序集中,我们可以在目录“%ProgramFiles%Microsoft ASP.NET\ASP.NET Web Stack 5\Packages” 中找到这个程序集。
具体来说,该程序集存在于子目录“Microsoft.AspNet.WebApi.Core.5.0.0\lib\net45” 中。

Web API体现在如下所示的ContactsController类型中。在该类型中,我们定义了Get、Post、Put和Delete这4个Action方法,它们分别实现了针对联系人的查询、添加、修改和删除操作。Action方法Get具有一个表示联系人ID的可缺省参数,如果该参数存在则返回对应的联系人,否则返回整个联系人列表。由于ASP.NET Web API默认实现了Action方法与HTTP方法的映射,所以方法名也体现了它们各自所能处理的请求必须采用的HTTP方法。

 using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using Common; namespace WebApi
{
public class ContactsController : ApiController
{
private static readonly List<Contact> Contacts; static ContactsController()
{
Contacts = new List<Contact>
{
new Contact
{
Id = "",
Name = "张三",
PhoneNum = "",
EmailAddress = "zhangsan@gmail.com",
Address = "add1"
},
new Contact
{
Id = "",
Name = "李四",
PhoneNum = "",
EmailAddress = "lisi@gmail.com",
Address = "add2"
}
};
} public IEnumerable<Contact> Get(string id = null)
{
return from contact in Contacts where contact.Id == id || string.IsNullOrWhiteSpace(id) select contact;
} public void Post(Contact contact)
{
Contacts.Add(contact);
} public void Put(Contact contact)
{
Contacts.Remove(Contacts.First(c => c.Id == contact.Id));
Contacts.Add(contact);
} public void Delete(string id)
{
Contacts.Remove(Contacts.First(c => c.Id == id));
}
}
}

三,新建一个ASP.NET Web项目并修改Global.asax.cs文件

除了在Application_Start中加入路由配置外,还需要在此项目中引用之前创建的WebApi项目

 using System;
using System.Web;
using System.Web.Http;
using System.Web.Routing; namespace WebHost
{
public class Global : HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Ignore("{resource}.axd/{*pathInfo}"); // 全局路由表HttpRouteCollection对象的扩展方法MapHttpRoute来完成
GlobalConfiguration.Configuration.Routes.MapHttpRoute("defaultApi", "api/{controller}/{id}", new {id = RouteParameter.Optional});
}
}
}

四,测试

用Chrome默认一application/xml的Content-Type返回,IE则默认以application/json的Content-Type返回;

我这边用Chrome测试的返回结果:

请求URL:http://localhost/webhost/api/contacts

<ArrayOfContact xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Common">
<Contact>
<Address>add1</Address>
<EmailAddress>zhangsan@gmail.com</EmailAddress>
<Id>001</Id>
<Name>张三</Name>
<PhoneNum>12345678</PhoneNum>
</Contact>
<Contact>
<Address>add2</Address>
<EmailAddress>lisi@gmail.com</EmailAddress>
<Id>002</Id>
<Name>李四</Name>
<PhoneNum>23456789</PhoneNum>
</Contact>
</ArrayOfContact>

请求URL:http://localhost/webhost/api/contacts/001

<ArrayOfContact xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Common">
<Contact>
<Address>add1</Address>
<EmailAddress>zhangsan@gmail.com</EmailAddress>
<Id>001</Id>
<Name>张三</Name>
<PhoneNum>12345678</PhoneNum>
</Contact>
</ArrayOfContact>

  

上一篇:java面向对象编程(三)--this


下一篇:ZH奶酪:【数据结构与算法】基础排序算法总结与Python实现