学习连接http://www.cnblogs.com/JustRun1983/p/3967757.html
Web Form和MVC现在还不能作为一个中间件集成到OWIN管道中,所有要么二选一,要么先后执行。二选一就是根据路由匹配选择执行方式,先后执行就是先执行Owin管道,后执行mvc。
第一部分:
直接表达式方法
[assembly: OwinStartup(typeof(OwinTest.Startup))] namespace OwinTest { public class Startup { public void Configuration(IAppBuilder app) { app.Run(context => { //context.Environment context.Response.ContentType = "text/plain"; return context.Response.WriteAsync("Hello world"); }); } } }
invoke方法
[assembly: OwinStartup(typeof(OwinWebForm.Startup))] namespace OwinWebForm { public class Startup { public void Configuration(IAppBuilder app) { app.Run(Invoke); } public Task Invoke(IOwinContext context) { context.Response.ContentType = "text/plain"; return context.Response.WriteAsync("hello world"); } } public class OwinApp2 { public static Task Invoke(IOwinContext context) { context.Response.ContentType = "text/plain"; return context.Response.WriteAsync("Hello World special"); } } }
使用自定义Middleware
public class Startup { public void Configuration(IAppBuilder app) { app.Use<CacheMiddleware>(); app.Use<HelloMiddleware>(); } }
public class HelloMiddleware : OwinMiddleware { public HelloMiddleware(OwinMiddleware next) : base(next) { } public override Task Invoke(IOwinContext context) { var response = "Hello World ! It is " + DateTime.Now; if (context.Environment.ContainsKey("caching.addToCache")) { var addToCache =context.Environment["caching.addToCache"] as Action<IOwinContext, string, TimeSpan>; addToCache(context, response, TimeSpan.FromSeconds(10)); } context.Response.Write(response); //return Task.FromResult(0); return Next.Invoke(context); } }
public class CacheMiddleware : OwinMiddleware { public CacheMiddleware(OwinMiddleware next) : base(next) { } private readonly IDictionary<string, CacheItem> _responseCache = new Dictionary<string, CacheItem>(); public override Task Invoke(IOwinContext context) { context.Environment["caching.addToCache"] = new Action<IOwinContext, string, TimeSpan>(AddToCache); var path = context.Request.Path.Value; if (!_responseCache.ContainsKey(path)) { return Next.Invoke(context); } var cacheItem = _responseCache[path]; if (cacheItem.ExpiryTime <= DateTime.Now) { return Next.Invoke(context); } context.Response.Write(cacheItem.Response); return Task.FromResult(0); //return Next.Invoke(context); } public void AddToCache(IOwinContext context, string response, TimeSpan cacheDuration) { _responseCache[context.Request.Path.Value] = new CacheItem { Response = response, ExpiryTime = DateTime.Now + cacheDuration }; } private class CacheItem { public string Response { get; set; } public DateTime ExpiryTime { get; set; } } }
第二部分:
a:在所有程序前调用,直接使用startup。
b:在部分路径下调用,需要在appSetting里设置,把自动开始关闭。
<add key="owin:AutomaticAppStartup" value="false" />
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); //RouteTable.Routes.MapOwinPath("/", app => // { // app.Use<CacheMiddleware>(); // app.Use<HelloMiddleware>(); // }); //owin 再其它路由配置之前,保证优先级 //RouteTable.Routes.MapOwinPath("/owin"); //RouteTable.Routes.MapOwinPath("/special", app => //{ // app.Run(OwinApp2.Invoke); //}); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); }
或
public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); //owin 再其它路由配置之前,保证优先级 RouteTable.Routes.MapOwinPath("/owin"); RouteTable.Routes.MapOwinPath("/special", app => { app.Run(OwinApp2.Invoke); }); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } }
注意:using Owin;