在 IIS 裏面部署 Owin,既能獲得 Owin 管道模型的靈活性和模塊特性,也能很好地利用 IIS 成熟的配置,Owin 程序將會跑在 ASP.NET request 的管道中。瀏覽器
首先建一個空的 Web 項目app
添加 Nuget 包 Microsoft.Owin.Host.SystemWeb函數
添加一個 Startup 類ui
替換 Startup.cs 的代碼this
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 運行查看結果spa
在 Self - Host 程序中,你的程序將會使用 HttpListener 建立一個進程來看成 Http Server 。3d
首先添加一個控制檯程序code
添加 NuGet 包 Microsoft.Owin.SelfHostorm
添加一個 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
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
參考地址: