zoukankan      html  css  js  c++  java
  • Owin与mvc

    学习链接

    http://www.cnblogs.com/JustRun1983/p/3967757.html

    第一部分:

    直接表达式方法

    [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;

  • 相关阅读:
    挑战程序设计竞赛 2.1 最基础的“穷竭搜索”
    HDU 5145 NPY and girls(莫队算法+乘法逆元)
    BZOJ 4300 绝世好题(位运算)
    HDU 5724 Chess(博弈论)
    BZOJ 1177 [Apio2009]Oil(递推)
    Codeforces 706D Vasiliy's Multiset(可持久化字典树)
    HDU 3374 String Problem (KMP+最小最大表示)
    POJ 2758 Checking the Text(Hash+二分答案)
    HDU 5782 Cycle(KMP+Hash)
    POJ 3450 Corporate Identity(KMP)
  • 原文地址:https://www.cnblogs.com/tgdjw/p/4623223.html
Copyright © 2011-2022 走看看