Web APi入门之Self-Host(二)
这篇来讲讲WebApi的自托管,WebApi可以托管到控制台/winform/服务上,并不是一定要依赖IIS才行。
1、首先新建控制台项目,在通过Nuget搜索Microsoft.AspNet.WebApi.SelfHost

2、建立实体类
public class Product
{
public int Id { set; get; }
public string Name { set; get; }
public string Description { set; get; }
}
3、创建WebApi控制器,继承ApiController
public class HomeController : ApiController
{
static List<Product> modelList = new List<Product>()
{
new Product(){Id=,Name="电脑",Description="电器"},
new Product(){Id=,Name="冰箱",Description="电器"},
}; //获取所有数据
[HttpGet]
public List<Product> GetAll()
{
return modelList;
} //获取一条数据
[HttpGet]
public Product GetOne(int id)
{
return modelList.FirstOrDefault(p => p.Id == id);
} //新增
[HttpPost]
public bool PostNew(Product model)
{
modelList.Add(model);
return true;
} //删除
[HttpDelete]
public bool Delete(int id)
{
return modelList.Remove(modelList.Find(p => p.Id == id));
} //更新
[HttpPut]
public bool PutOne(Product model)
{
Product editModel = modelList.Find(p => p.Id == model.Id);
editModel.Name = model.Name;
editModel.Description = model.Description;
return true;
}
}
4、在Program的Main方法中加上如下代码
static void Main(string[] args)
{
var config = new HttpSelfHostConfiguration("http://localhost:5000"); //配置主机 config.Routes.MapHttpRoute( //配置路由
"API Default", "api/{controller}/{id}",
new { id = RouteParameter.Optional }); using (HttpSelfHostServer server = new HttpSelfHostServer(config)) //监听HTTP
{
server.OpenAsync().Wait(); //开启来自客户端的请求
Console.WriteLine("Press Enter to quit");
Console.ReadLine();
}
}
5、本人机器是win10,如果直接运行是会报错的

解决方法:打开项目路径找到项目名.exe文件右键以管理员身份运行

6、通过url方式访问 http://localhost:5000/api/home

通过winform、服务等方式托管的话本篇不再讲述,方式都类似,我觉得WebApi托管在IIS上是好于其他方式的,用控制台的话演示还是不错的。
下篇讲下WebApi跨域。
Web APi入门之Self-Host(二)的更多相关文章
- Web API 入门 二 媒体类型
还是拿上面 那篇 Web API 入门 一 的那个来讲 在product类中加一个时间属性
- Web API 入门指南 - 闲话安全
Web API入门指南有些朋友回复问了些安全方面的问题,安全方面可以写的东西实在太多了,这里尽量围绕着Web API的安全性来展开,介绍一些安全的基本概念,常见安全隐患.相关的防御技巧以及Web AP ...
- 转载-Web API 入门
An Introduction to ASP.NET Web API 目前感觉最好的Web API入门教程 HTTP状态码 Web API 强势入门指南 Install Mongodb Getting ...
- Web API入门指南(安全)转
安全检测的工具站点:https://www.owasp.org/index.php/Category:Vulnerability_Scanning_Tools Web API入门指南有些朋友回复问了些 ...
- Web API 入门指南
Web API 入门指南 - 闲话安全2013-09-21 18:56 by 微软互联网开发支持, 231 阅读, 3 评论, 收藏, 编辑 Web API入门指南有些朋友回复问了些安全方面的问题,安 ...
- (转)Web API 入门指南 - 闲话安全
原文地址:http://www.cnblogs.com/developersupport/p/WebAPI-Security.html Web API入门指南有些朋友回复问了些安全方面的问题,安全方面 ...
- 【ASP.NET Web API教程】1 ASP.NET Web API入门
原文 [ASP.NET Web API教程]1 ASP.NET Web API入门 Getting Started with ASP.NET Web API第1章 ASP.NET Web API入门 ...
- Web APi入门之Self-Host寄宿及路由原理(二)
前言 刚开始表面上感觉Web API内容似乎没什么,也就是返回JSON数据,事实上远非我所想,不去研究不知道,其中的水还是比较深,那又如何,一步一个脚印来学习都将迎刃而解. Self-Host 我们知 ...
- Web API入门二(实例)
学习编程的最好方法就是实例,本人用的是VS2015 1.创建ASP.NET Web空项目 点击确定后即创建了空"WebApi"项目 2.下面,我们需要使用NuGet包管理器添加最新 ...
随机推荐
- [转]从头开始 GAN
1 前言 GAN的火爆想必大家都很清楚了,各种GAN像雨后春笋一样冒出来,大家也都可以名正言顺的说脏话了[微笑脸].虽然目前GAN的酷炫应用还集中在图像生成上,但是GAN也已经拓展到NLP,Robot ...
- Hdu3022 Sum of Digits
Sum of Digits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- Codeforces 717.F Heroes of Making Magic III
F. Heroes of Making Magic III time limit per test 3 seconds memory limit per test 256 megabytes inpu ...
- linux python3获取ip地址
一.不带参数 #!/usr/bin/python # -*- coding: UTF-8 -*- import os def get_ip(): #注意外围使用双引号而非单引号,并且假设默认是第一个网 ...
- 高可用rabbitmq集群服务部署步骤
消息队列是非常基础的关键服务,为保证公司队列服务的高可用及负载均衡,现通过如下方式实现: RabbitMQ Cluster + Queue HA + Haproxy + Keepalived 3台ra ...
- [LeetCode] 29. Divide Two Integers ☆☆
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- spring boot 2.0.3+spring cloud (Finchley)7、服务链路追踪Spring Cloud Sleuth
参考:Spring Cloud(十二):分布式链路跟踪 Sleuth 与 Zipkin[Finchley 版] Spring Cloud Sleuth 是Spring Cloud的一个组件,主要功能是 ...
- 2.aop中几个注解的含义
参考地址:http://elim.iteye.com/blog/2395255
- 分治法:三维偏序问题之CDQ分治
我怀疑那个k是用来定界限用的 #include <cstdio> #include <cstring> #include <algorithm> using nam ...
- HTML 5 Web 存储:localStorage和sessionStorage
本文内容摘自http://www.w3school.com.cn/ 在客户端存储数据 HTML5 提供了两种在客户端存储数据的新方法: localStorage - 没有时间限制的数据存储 sessi ...