ASP.NET Web API 不需要 IIS,直接使用控制台程序可以实现。

一、创建一个新的控制台程序,项目名为 HostApi

二、设置目标框架为.NET Framework 4

三、NuGet添加引用  Microsoft.AspNet.WebApi.SelfHost

四、添加一个 Product Model类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace HostApi
{
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public string Category { get; set; }
public decimal Price { get; set; }
}
}

五、添加一个名为ProductsController的公共类。从System.Web.Http.ApiController派生此类.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web.Http; namespace HostApi
{
public class ProductsController : ApiController
{
Product[] products = new Product[]
{
new Product { Id = , Name = "Tomato Soup", Category = "Groceries", Price = },
new Product { Id = , Name = "Yo-yo", Category = "Toys", Price = 3.75M },
new Product { Id = , 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));
}
}
}

六、 托管 Web API

打开 Program.cs 文件,并添加以下使用语句:

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

七、修改Main方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using System.Web.Http;
using System.Web.Http.SelfHost; namespace HostApi
{
class Program
{
static void Main(string[] args)
{
var config = new HttpSelfHostConfiguration("http://localhost:1234"); 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();
}
}
}
}

直接F5运行,然后打开浏览器访问:

获取所有数据  http://localhost:1234/api/products

获取内码为1的数据  http://localhost:1234/api/products/1

查询category=的数据  http://localhost:1234/api/products?category=Groceries

返回:

参考 http://www.asp.net/web-api/overview/older-versions/self-host-a-web-api

Self-Host Web API 学习笔记的更多相关文章

  1. ASP.NET MVC Web API 学习笔记---第一个Web API程序

    http://www.cnblogs.com/qingyuan/archive/2012/10/12/2720824.html GetListAll /api/Contact GetListBySex ...

  2. 【转载】ASP.NET MVC Web API 学习笔记---联系人增删改查

    本章节简单介绍一下使用ASP.NET MVC Web API 做增删改查.目前很多Http服务还是通过REST或者类似RESP的模型来进行数据操作的.下面我们通过创建一个简单的Web API来管理联系 ...

  3. 【转载】ASP.NET MVC Web API 学习笔记---第一个Web API程序

    1. Web API简单说明 近来很多大型的平台都公开了Web API.比如百度地图 Web API,做过地图相关的人都熟悉.公开服务这种方式可以使它易于与各种各样的设备和客户端平台集成功能,以及通过 ...

  4. Web API学习笔记(Python实现)

    参考指南: Web API入门指南 http://www.cnblogs.com/guyun/p/4589115.html 用Python写一个简单的Web框架 http://www.cnblogs. ...

  5. ASP.NET MVC Web API 学习笔记---Web API概述及程序示例

    1. Web API简单说明 近来很多大型的平台都公开了Web API.比如百度地图 Web API,做过地图相关的人都熟悉.公开服务这种方式可以使它易于与各种各样的设备和客户端平台集成功能,以及通过 ...

  6. ASP.NET MVC Web API 学习笔记---联系人增删改查

    本章节简单介绍一下使用ASP.NET MVC Web API 做增删改查. 目前很多Http服务还是通过REST或者类似RESP的模型来进行数据操作的. 下面我们通过创建一个简单的Web API来管理 ...

  7. ASP.NET MVC Web API 学习笔记---第一个Web API程序【转】

    http://www.cnblogs.com/qingyuan/archive/2012/10/12/2720824.html 1. Web API简单说明 近来很多大型的平台都公开了Web API. ...

  8. ASP.NET MVC Web API 学习笔记---第一个Web API程序---近来很多大型的平台都公开了Web API

    1. Web API简单说明 近来很多大型的平台都公开了Web API.比如百度地图 Web API,做过地图相关的人都熟悉.公开服务这种方式可以使它易于与各种各样的设备和客户端平台集成功能,以及通过 ...

  9. Java web与web gis学习笔记(二)——百度地图API调用

    系列链接: Java web与web gis学习笔记(一)--Tomcat环境搭建 Java web与web gis学习笔记(二)--百度地图API调用 JavaWeb和WebGIS学习笔记(三)-- ...

随机推荐

  1. 精简的网站reset 和 css通用样式库

    参考链接:http://www.zhangxinxu.com/wordpress/2010/07/我是如何对网站css进行架构的/ reset.css body{ line-height:1.4; c ...

  2. github安装&初探

    主要记录安装中的问题: 1 centos 5和6的epel源需要不同的包来更新 Centos 5.x wget http://dl.fedoraproject.org/pub/epel/5/x86_6 ...

  3. With great power comes great responsibility

    We trust you have received the usual lecture from the local SystemAdministrator. It usually boils do ...

  4. Jquery点击本身,修改出本身之外的其他同级元素的样式

    1.引用Jquyer库 2.Jquery代码: <script type="text/javascript"> $(function () { slidColor('d ...

  5. yii模块下面的组件

    模块的定义就不写了,直接进入主题看目录和文件: application/modules/client/controllers/UserController.php <?php class Use ...

  6. MapKit/CoreLocation框架 总结

    MapKit/CoreLocation框架 /*英译 core:核心 track:踪迹 current:当前 statellite:卫星 hybird:混合  region:范围 annotation ...

  7. Note1: windows下Python IDE(Eclipse+Pydev)

    毕设想做一个个性化音乐推荐系统,真的是从零开始学起,通过<推荐系统实践>大致地了解了一些基础知识,目前在啃<集体智慧编程>. 选用的开发环境是 Eclipse+Pydev . ...

  8. Datatables JQuery插件

    Datatables是一款jquery表格插件.它是一个高度灵活的工具,可以将任何HTML表格添加高级的交互功能. 分页,即时搜索和排序 几乎支持任何数据源:DOM, javascript, Ajax ...

  9. 【CentOS】正则表达式

    1.grep  [-cinvABC]  'word'  filename -c :打印符合要求的行数 --color:显示颜色 -i :忽略大小写(ignore) -n :在输出符合要求的行的同时连同 ...

  10. 前台处理json字符串的几种方法(转)

    原文地址http://www.css88.com/archives/3919 比如我有两个变量,我要将a转换成字符串,将b转换成JSON对象: var a={"name":&quo ...