Asp.Net Web API 2第九课——自承载Web API

原文:Asp.Net Web API 2第九课——自承载Web API

前言

阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/3446289.html

Asp.Net Web API可以需要IIS。你可以在你自己的主机上来承载一个Web API。

本教程来展示在控制台应用程序中来承载一个Web API。使用的开发工具为VS2013。

本文示例代码下载链接http://pan.baidu.com/s/1d56zf

创建一个控制台应用程序

 

这里我默认的Framework版本为4.5的。然后通过Nuget来下载安装Microsoft.AspNet.WebApi.SelfHost。

创建Model和Controller

 首先添加一个public公共类Product。

    public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Category { get; set; }
        public decimal Price { get; set; }
    }

然后添加一个public公共类ProductsController,并且这个类继承自System.Web.Http.ApiController。记得添加扩展引用System.Web.Http

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;

namespace SelfHost
{
    public class ProductsController:ApiController
    {
        Product[] products = new Product[]
        {
            new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 },
            new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M },
            new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M }
        };
        public IEnumerable<Product> GetAllProducts()
        {
            return products;
        }
        public Product GetProductById(int id)
        {
            var product = products.FirstOrDefault((p) => p.Id == id);
            if (product == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            return product;
        }
        public IEnumerable<Product> GetProductsByCategory(string category)
        {
            return products.Where(p => string.Equals(p.Category, category,
                    StringComparison.OrdinalIgnoreCase));
        }
    }
}

这个控制器定义了三个Get方法:

承载Web API

 打开Program.cs,然后添加如下使用语句:

using System.Web.Http;
using System.Web.Http.SelfHost; 

当然如果你没有引用,还是先要添加引用的(另外还有System.Net.Http)。然后添加如下代码到Program.cs里:

var config = new HttpSelfHostConfiguration("http://localhost:8080");

config.Routes.MapHttpRoute(
    "API Default", "api/{controller}/{id}",
    new { id = RouteParameter.Optional });

using (HttpSelfHostServer server = new HttpSelfHostServer(config))
{
    server.OpenAsync().Wait();
    Console.WriteLine("Press Enter to quit.");
    Console.ReadLine();
}

现在你可以运行控制台程序了。

现在可以通过URI来简单测试Web API的正确性。

(可选的)添加一个HTTP URL命名空间保留(没遇到这个问题,暂未测试)

 这个应用程序侦听到"http://localhost:8080"。在默认情况下,侦听一个特殊的HTTP URL是需要管理员权限的。当你运行上面的控制台应用程序的时候,你可能会得到这样的一个错误:"HTTP could not register URL http://+:8080",这儿有两种方式去避免这个错误:

  1.以管理员身份运行Visual Studio。

  2.使用Netsh.exe给与你的账号权限去保留这个URL。

若要使用Netsh.exe,以管理员身份打开命令提示框,并键入以下命令:

netsh http add urlacl url=http://+:8080/ user=machine\username

其中machine\username是您的用户帐户。

当你使用完自托管的时候,最好是确定删除这个保留的URL。

netsh http delete urlacl url=http://+:8080/

通过客户端应用程序来调用Web API

让我们来写一个简单的控制台应用程序来调用Web API。

添加一个控制台应用程序,并命名为"ClientApp"。

同样的通过Nuget来添加Microsoft.AspNet.WebApi.Client。

 

当然还需要应用SelfHost这个项目。

打开ClientApp项目的Program.cs文件,添加如下using语句

using System.Net.Http; 

添加一个静态的HttpClient实例:

namespace ClientApp
{
    class Program
    {
        static HttpClient client = new HttpClient();
        static void Main(string[] args)
        {
        }
    }
}

添加三个方法 获得所有产品列表信息,通过ID获得指定产品信息,通过目录获得产品列表信息。

static void ListAllProducts()
{
    HttpResponseMessage resp = client.GetAsync("api/products").Result;
    resp.EnsureSuccessStatusCode();

    var products = resp.Content.ReadAsAsync<IEnumerable<SelfHost.Product>>().Result;
    foreach (var p in products)
    {
        Console.WriteLine("{0} {1} {2} ({3})", p.Id, p.Name, p.Price, p.Category);
    }
}

static void ListProduct(int id)
{
    var resp = client.GetAsync(string.Format("api/products/{0}", id)).Result;
    resp.EnsureSuccessStatusCode();

    var product = resp.Content.ReadAsAsync<SelfHost.Product>().Result;
    Console.WriteLine("ID {0}: {1}", id, product.Name);
}

static void ListProducts(string category)
{
    Console.WriteLine("Products in '{0}':", category);

    string query = string.Format("api/products?category={0}", category);

    var resp = client.GetAsync(query).Result;
    resp.EnsureSuccessStatusCode();

    var products = resp.Content.ReadAsAsync<IEnumerable<SelfHost.Product>>().Result;
    foreach (var product in products)
    {
        Console.WriteLine(product.Name);
    }
}

每个方法遵循相同的模式:

  1.调用HttpClient.GetAsync来发送一个HTTP Get请求到适当的URI。

  2.调用HttpResponseMessage.EnsureSuccessStatusCode ,如果HTTP响应状态是一个错误码,那么这个方法将抛出一个异常。

  3.调用ReadAsAsync<T> 反序列化一个来自HTTP响应的CLR类型。 这个方法是一个扩展方法,被定义在System.Net.Http.HttpContentExtensions


GetAsync 和ReadAsAsync 这两个方法都是异步方法。它们通过返回Task 对象来代表异步操作。获取Result属性阻止线程,直到操作完成。

在调用这些方法之前, BaseAddress 上的属性设置为"http://localhost:8080"的 HttpClient 实例。例如:

 

static void Main(string[] args)
{
    client.BaseAddress = new Uri("http://localhost:8080");

    ListAllProducts();
    ListProduct(1);
    ListProducts("toys");

    Console.WriteLine("Press Enter to quit.");
    Console.ReadLine();
}

接下来,进行测试。设置启动项目。

预测输出内容,应该会输出以下内容:

1 Tomato Soup 1.0 (Groceries)
2 Yo-yo 3.75 (Toys)
3 Hammer 16.99 (Hardware)
ID 1: Tomato Soup
Products in 'toys':
Yo-yo
Press Enter to quit.

运行程序,查看结果

总结

感觉还是比较简单的吧,就这样一步一步的下来还是没什么阻碍的。

本文的参考链接http://www.asp.net/web-api/overview/hosting-aspnet-web-api/self-host-a-web-api

本文已同步到Web API系列导航 http://www.cnblogs.com/aehyok/p/3446289.html

本文示例代码下载链接http://pan.baidu.com/s/1d56zf

时间: 2024-08-03 13:47:15

Asp.Net Web API 2第九课——自承载Web API的相关文章

Asp.Net Web API 2第二课——CRUD操作

原文:Asp.Net Web API 2第二课--CRUD操作 Asp.Net Web API 导航   Asp.Net Web API第一课--入门http://www.cnblogs.com/aehyok/p/3432158.html 前言 CRUD代表着 Create.Read.Update.Delete,这是四个基本的数据库操作.许多HTTP服务模型也通过Rest或者Rest-like APIs实现CRUD操作.       在本教程中,我将建立一个非常简单的Web API来管理一个产品

Asp.Net Web API 2第一课——入门

原文:Asp.Net Web API 2第一课--入门 前言 Http不仅仅服务于Web Pages.它也是一个创建展示服务和数据的API的强大平台.Http是简单的.灵活的.无处不在的.你能想象到几乎任何的平台都会有HTTP服务库.HTTP服务可以涉及到范围广泛的客户端,包括浏览器.各种移动设备和传统的桌面应用程序. Asp.Net Web API是在.NET Framework框架上用于建立Web APIs的一个框架.在这个教程中,你将会使用Asp.Net Web API框架来创建一个能够返

Asp.Net Web API 2第十课——使用OWIN自承载Web API

原文:Asp.Net Web API 2第十课--使用OWIN自承载Web API 前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/3446289.html 本教程主要来展示在控制台应用程序中如何通过OWIN来承载Web API. Open Web Interface for .NET (OWIN)在Web服务器和Web应用程序之间建立一个抽象层.OWIN将网页应用程序从网页服务器分离出来,然后将

【ASP.NET Web API教程】2 创建各种Web API

原文 [ASP.NET Web API教程]2 创建各种Web API Chapter 2: Creating Web APIs第2章 创建各种Web API 本文引自:http://www.asp.net/web-api/overview/creating-web-apis In this chapter, you'll learn:本章你将学习: End-to-end tutorials and samples for ASP.NET Web APIASP.NET Web API的端对端教程

【ASP.NET Web API教程】2.4 创建Web API的帮助页面

原文:[ASP.NET Web API教程]2.4 创建Web API的帮助页面 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的内容. 2.4 Creating a Help Page for a Web API 2.4 创建Web API帮助页面 本文引自:http://www.asp.net/web-api/overview/creating-web-apis/creating-a-help-page-for-a-web-api By

【翻译】在Visual Studio中使用Asp.Net Core MVC创建你的第一个Web API应用(一)

HTTP is not just for serving up web pages. It's also a powerful platform for building APIs that expose services and data. HTTP is simple, flexible, and ubiquitous. Almost any platform that you can think of has an HTTP library, so HTTP services can re

《精通 ASP.NET MVC 5》----1.2 当今的Web开发

1.2 当今的Web开发 自Web Form首次发布以来,Web开发技术一直在迅速发展,集中表现在几个不同的方面. 1.2.1 Web标准与REST 近年来,朝着Web标准兼容的趋势在增长.Web网站也在比以往更为广泛的设备和浏览器上使用,Web标准(HTML.CSS.JavaScript等)使人们有望在不同的设备上享受一致的浏览体验.现代Web平台不可忽视这种商业环境和开发者对Web标准兼容的追求. HTML 5已经进入主流运用范围,并给Web开发人员提供了丰富的功能,让客户端能够执行一些以前

也不贴上,用ASP开发基于Windows NT Server和IIS的Web应用

iis|server|web|window 用ASP开发基于Windows NT Server和IIS的Web应用 国防科工委指挥技术学院 韩正清 钱明 摘要: 本文比较了CGI ISAPI与ASP在结构和性能方面的差异,并介绍了用ASP开发WEB 应用的一般过程.关键词: ASP CGI ISAPI IIS一.前言Internet/Intranet和WEB技术的出现和发展,为信息的交换和共享提供了快捷有效的方法,但Internet/Intranet上提供的信息多以静态网页为主,对于一个WEB应

ASP.NET中根据XML动态创建并使用WEB组件(三)

asp.net|web|xml|创建|动态 ASP.NET中根据XML动态创建使用WEB组件 (三) 作者:厉铁帅 四.使用动态创建的WEB组件 如果在动态创建了WEB组件后,要使用该组件,可使用如下语句 String sequencelabelID="sequencedataTB"+icount.ToString(); Label sequencelabel=(Label)myPlaceHolder.FindControl(sequencelabelID); Sequencelabe