每一个 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