第一个ASP.NET Web API (C#)程序

本文翻自http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api

绝对手工制作,如有雷同,实属巧合。

转载请注明。

由于能力有限本文可能出错,请见谅,请指出。

第一章 开始(上)

在这里,你应该学会如何构建一个使用ASP.NET Web API的HTTP服务。

1 你的第一个WEB API

HTTP不仅能够服务于网站,还是一个可以用来构建API(被用来提供服务和数据的)的强大的平台。HTTP简单、灵活、易用。几乎所有的平台都可以有HTTP库,所以HTTP服务可以响应广大的客户端,包括浏览器(电脑吧?)、移动设备、传统的桌面设备。

ASP.NET Web API是一个用来构建web API的框架(基于.NET框架)。在这次学习中,你可以用ASP.NET Web API创建一个能够返回产品集合的web API。前端页面使用jQuery来显示结果。

截图暂无 ,等文章发表完吧.....

--------等待截图-------

*

系统条件,就是Visual Studio 2012或者Visual Studio 2010+ASP.NET MVC 4。原作者用的是isual Studio Express 2012。我用的是2010

This tutorial requires Visual Studio with ASP.NET MVC 4. You can use any of the following:

  • Visual Studio 2012
  • Visual Studio Express 2012 for Web
  • Visual Studio 2010 with ASP.NET MVC 4 installed.
  • Visual Web Developer 2010 Express with ASP.NET MVC 4 installed.

If you are using Visual Studio 2010 or Visual Web Developer 2010 Express, you will need to install ASP.NET MVC 4 separately. The screenshots in this tutorial show Visual Studio Express 2012 for Web.

*

打开VS2010--》开始菜单--》新建--》项目--》选择 ASP.NET MVC 4 Web Application,之后改名字---HelloWebAPI

New ASP.NET MVC 4 Project 对话框, 选择Web API

添加一个Model

model用来在应用中保存、展现数据。ASP.NET Web API能够自动序列化你的model成json、xml或其他形式,然后把序列化后的数据写入到HTTP response message的body中(这块不好说明白,就是用HTTP response发送吧)。客户端一旦读取到序列化的数据,就反序列化数据。大部分客户端能够转换xml、json。客户端怎么知道要转换成什么格式呢?它根据的是保存在HTTP request的Accept header的信息。注:这里不太懂,昨天大致了解了下,可以参考http://www.cnblogs.com/lexus/archive/2012/02/21/2360944.html(HTTP request)和http://www.cnblogs.com/lzrabbit/archive/2013/03/19/2948522.html及1L留言请留意。

我们继续。

在Models文件夹上右键选择添加类,命名为Product

上代码

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 
 6 namespace HelloWebAPI.Models
 7 {
 8     public class Product
 9     {
10         public int Id { get; set; }
11         public string Name { get; set; }
12         public string Category { get; set; }
13         public decimal Price { get; set; }
14     }
15 }
View Code

添加Controller

controller控制转发HTTP requests。新建的工程已经创建了两个controller

HomeController是一般的ASP.NET MVC controller。

ValuesController是WebAPI controller实例。

注:如果你使用过ASP.NET MVC,那就很熟悉controllers了。ASP.NET MVC controller和WebAPI controller很像,WebAPI controller继承自ApiController,不再返回View而是data。

删除ValuesController,右键Controllers目录,选择添加Controller

重命名为ProductsController,选择Empty API Controller

Controllers目录名称可以随意修改。

还是代码。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Net;
 5 using System.Net.Http;
 6 using System.Web.Http;
 7 using System.Collections;
 8 using HelloWebAPI.Models;
 9 
10 namespace HelloWebAPI.Controllers
11 {
12     public class ProductsController : ApiController
13     {
14         Product[] products = new Product[]
15         {
16             new Product { Id = 1, Name = "Tomato", Category = "Groceries", Price = 1 }, 
17             new Product { Id = 2, Name = "Yo", Category = "Toys", Price = 3.75M }, 
18             new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M } 
19         };
20 
21         public IEnumerable<Product> GetAllProducts()
22         {
23             return products;
24         }
25 
26         public Product GetProductById(int id)
27         {
28             var product = products.FirstOrDefault(x => x.Id == id);
29             if (product == null)
30             {
31                 throw new HttpResponseException(HttpStatusCode.NotFound);
32             }
33             return product;
34         }
35     }
36 }
View Code

using顺序和原著不太一样。

为了例子简答,产品保存在一个数组里。当然在实际项目中,你应该查询数据库或者用其他数据源。

controller定义了两种方法返回产品。

  •  GetAllProductsIEnumerable<Product> 类型返回所有的产品.
  •  GetProductById用ID查找某一特定产品。

controller方法和URI一一对应,对应关系为:

Controller 方法URI
GetAllProducts /api/products
GetProductById /api/products/id

客户端能够通过发送HTTP GET请求到URI来调用方法。现在,我们来看看结果。

从客户端调用我们的Web API

你能够使用任意HTTP客户端吧调用你的web API。现在我们用一个浏览器调用。

按下F5,ASP.NET Development Server启动,地址栏输入http://localhost:端口号/api/products/,我的浏览器是IE

 

点击打开--》记事本打开,结果如下。

 

使用火狐,结果如下。

<ArrayOfProduct><Product><Category>Groceries</Category><Id>1</Id><Name>Tomato</Name><Price>1</Price></Product><Product><Category>Toys</Category><Id>2</Id><Name>Yo</Name><Price>3.75</Price></Product><Product><Category>Hardware</Category><Id>3</Id><Name>Hammer</Name><Price>16.99</Price></Product></ArrayOfProduct>

结果不同的原因就是IE和火狐发送了不同Accept headers

请自行尝试http://localhost:xxxx/api/products/1的效果。

 

posted @ 2013-06-17 21:39  JustDotNet  阅读(564)  评论(0编辑  收藏  举报