【Owin 学习系列】1. 第一个 Owin 程序
2017-08-11 15:38
429 查看
IIS 中的 Owin
在 IIS 里面部署 Owin,既能得到 Owin 管道模型的灵活性和模块特性,也能很好地利用 IIS 成熟的配置,Owin 程序将会跑在 ASP.NET request 的管道中。首先建一个空的 Web 项目
添加 Nuget 包 Microsoft.Owin.Host.SystemWeb
添加一个 Startup 类
替换 Startup.cs 的代码
using System; using System.Threading.Tasks; using Microsoft.Owin; using Owin; [assembly: OwinStartup(typeof(OwinDemo.Startup))] namespace OwinDemo { public class Startup { public void Configuration(IAppBuilder app) { // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888 app.Run(context=> { context.Response.ContentType = "text/plain"; return context.Response.WriteAsync("Hello, world."); }); } } }
F5 运行查看结果
控制台 Self-Host OWIN
在 Self - Host 程序中,你的程序将会使用 HttpListener 创建一个进程来当作 Http Server 。首先添加一个控制台程序
添加 NuGet 包 Microsoft.Owin.SelfHost
添加一个 Owin Startup.cs 文件
替换 Startup.cs 文件内容
using Microsoft.Owin; using Owin; [assembly: OwinStartup(typeof(OwinConsoleDemo.Startup))] namespace OwinConsoleDemo { public class Startup { public void Configuration(IAppBuilder app) { // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888 app.Run(context=> { context.Response.ContentType = "text/plain"; return context.Response.WriteAsync("Hello , this is console appliaction from self owin."); }); } } }
修改控制台程序 Main 函数代码
static void Main(string[] args) { using (Microsoft.Owin.Hosting.WebApp.Start<Startup>("http://localhost:9000")) { Console.WriteLine("Press [enter] to quit..."); Console.ReadLine(); } }
F5 运行,然后浏览器访问 http://localhost:9000
添加 Owin Diagnostics
Microsoft.Owin.Diagnostics 包包含了一个能捕获未处理的异常的中间件,并且能把错误详情显示在一个 Html 页面中。首先在 Nuget 包里安装 Microsoft.Owin.Diagnostics
然后替换 Startup.cs 代码
using System; using Microsoft.Owin; using Owin; [assembly: OwinStartup(typeof(OwinDemo.Startup))] namespace OwinDemo { public class Startup { public void Configuration(IAppBuilder app) { app.UseErrorPage(); // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888 app.Run(context=> { if((context.Request.Path.ToString() == "/fail")) { throw new Exception("This is Owin Diagnostics Test."); } context.Response.ContentType = "text/plain"; return context.Response.WriteAsync("Hello, world."); }); } } }
F5 运行,地址栏输入 http://localhost:56764/fail ,就可以在错误页面看到详细的错误信息
源代码链接:
链接: http://pan.baidu.com/s/1c2w7Kuk 密码: s6h6
参考地址:
https://docs.microsoft.com/zh-cn/aspnet/aspnet/overview/owin-and-katana/getting-started-with-owin-and-katana
相关文章推荐
- 【Owin 学习系列】1. 第一个 Owin 程序
- 【Owin 学习系列】1. 第一个 Owin 程序
- 【Solr4.7.0学习笔记】系列之一:我的第一个Solr程序
- 【SignalR学习系列】2. 第一个SignalR程序
- WebGL学习系列-第一个程序
- 【SignalR学习系列】2. 第一个SignalR程序
- Go学习系列――第一个 Go程序
- Eclipse Rcp学习系列02—— 第一个Rcp程序
- hadoop学习系列1之第一个MAPREDUCE程序
- 【SignalR学习系列】2. 第一个SignalR程序
- SpringMVC4 学习笔记(七) 【第一个注解小程序】
- vuejs学习系列-第一个小demo
- php基础系列:从用户登录处理程序学习mysql扩展基本操作
- 一步一步学习sharepoint2010 workflow 系列第一部分:sharepoint工作流简介 第2章 你的第一个工作流(Your first workflow)
- 和大彪一起来学习-SpringMvc之第一回(框架了解和第一个SpringMvc程序) 分类: SpringMvc 2015-01-14 21:08 635人阅读 评论(0) 收藏
- Java 学习记录(二) 从零开始,第一个java程序
- 基于Cocos2d-x学习OpenGL ES 2.0系列——你的第一个三角形(1)
- 【学习ARToolkit小记之八】 开发第一个ARToolkit程序(Developing your First Application)(续2)
- Java学习第一天—让自己的第一个程序跑起来
- OpenGL入门学习——第一课 编写第一个OpenGL程序