每一個 Owin 程序都有 startup 類,在這個 startup 類裏面你能夠指定應用程序管道模型中的組件。你能夠經過不一樣的方式來鏈接你的 startup 類和運行時,這些取決於你選擇的宿主模型(OwinHost, IIS, and IIS-Express)。web
你能夠經過下面幾種方式來鏈接你的 startup 類和宿主程序。瀏覽器
[assembly: OwinStartup(typeof(OwinDemo.Startup))]
<appSettings>
<add key="owin:appStartup" value="OwinDemo.Startup2" />
</appSettings>
startup.cs 代碼app
using System; 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."); }); } } public class Startup2 { 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 Owin startup class 2."); }); } } }
F5 運行之後會進入 startup2 類,能夠經過瀏覽器看到結果。ui
你也在配置文件中指定 startup 類的別名,同時也要在 OwinStartup 特性裏設定,而後就會根據別名和 OwinStartup 特性找到對應的 startup 類。this
<appSettings> <add key="owin:appStartup" value="ProductionConfiguration" /> </appSettings>
using System; using Microsoft.Owin; using Owin; [assembly: OwinStartup("ProductionConfiguration", typeof(OwinDemo.Startup2))] 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."); }); } } public class Startup2 { 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 Owin startup class 2."); }); } } }
若是要關閉 OWIN startup 發現,那麼只須要在 appSetting 裏面加入下面的代碼spa
<add key="owin:AutomaticAppStartup " value="false" />
指定 Owin startup 類的 Configuration 方法code
<add key="owin:appStartup" value="OwinDemo.Startup2.ConfigurationTwo" />
public class Startup2 { 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 Owin startup class 2."); }); } public void ConfigurationTwo(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 Owin startup class 2 and ConfigurationTwo."); }); } }
F5 運行之後能夠看到結果orm
web.config 配置文件裏有多個 owin:appStartup 值,那麼會啓用最後一個配置 OwinDemo.Startup2 。blog
<appSettings> <add key="owin:appStartup" value="OwinDemo.Startup2.ConfigurationTwo" /> <add key="owin:appStartup" value="OwinDemo.Startup2" /> </appSettings>
Nuget 裏安裝 OwinHost開發
導航到你的應用程序文件夾(包含了 web.config 的文件夾),而後運行 Owinhost.exe
..\packages\Owinhost<Version>\tools\Owinhost.exe
最後訪問 http://localhost:5000/ ,就能夠看到效果了。
也能夠經過指定 OwinHost.exe 後面的參數訪問不一樣的 startup 類
..\packages\OwinHost.3.1.0\tools\Owinhost.exe OwinDemo.Startup2.ConfigurationTwo
源代碼連接:
連接: http://pan.baidu.com/s/1bOfTRC 密碼: xfhk
參考連接:
https://docs.microsoft.com/zh-cn/aspnet/aspnet/overview/owin-and-katana/owin-startup-class-detection