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包管理器添加最新 ...
随机推荐
- jsp 项目中 web.xml 的作用
每个 web 应用的 WEB-INF 路径下(而且必须位于该路径)的 web.xml 文件被称为配置描述符. 对于 java web 应用而言,WEB-INF 是一个特殊的文件夹,web 容器会包含该 ...
- bzoj 1510 [POI2006]Kra-The Disks 二分
1510: [POI2006]Kra-The Disks Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 466 Solved: 272[Submit][ ...
- Qt ------ stylesheet 样式
1.所有的窗口组件都可以用 setStyleSheet() 设置样式 2.使用样式,显示效果可以不受平台影响,比如保证window 7 和 linux 显示效果是一样的 QVariant 如果 sty ...
- django 配置xamdin遇到的坑
是在 Django==1.11.7 这个版本下配置的,需要说明的是,不是通过pip install xadmin方式安装的 在github上下载的xadmin源码包,需要在项目的根目录下创建extra ...
- 跟我一起写Makefile(七)
make 的运行—————— 一般来说,最简单的就是直接在命令行下输入make命令,make命令会找当前目录的makefile来执行,一切都是自动的.但也有时你也许只想让make重编译某些文件,而不是 ...
- java类的静态属性值获取
获取某个类实例的静态属性: public class ErrorCode { private String code; private String message; private ErrorCod ...
- Vue.js基础 笔记
Vue.js的声明: < script src = “https://unpkg.com/vue” ></ script > el:值可以是CSS选择符.HTML元素.或者是返 ...
- jieba文本分词,去除停用词,添加用户词
import jieba from collections import Counter from wordcloud import WordCloud import matplotlib.pyplo ...
- CSS3的新属性
1.圆角矩形 .border_radius_test{ border-radius:25px; -moz-border-radius:25px; } 数值越大越圆 2.容器阴影 .box_shadow ...
- MySQl学习-——Mysql体系结构与Mysql存储引擎
Mysql体系结构与Mysql存储引擎 Mysql体系结构 mysql体系结构图: