Fork me on GitHub

Owin 自定义中间件

/// <summary>
    /// 自定义的中间件
    /// </summary>
    public class CustomMiddleware : OwinMiddleware
    {
        CustomMiddlewareParameters _parameter;
        public CustomMiddleware(OwinMiddleware next, CustomMiddlewareParameters parameter) : base(next)
        {
            _parameter = parameter;
        }
        public override Task Invoke(IOwinContext c)
        {
            if (Next != null)
            {
                var msg = Encoding.UTF8.GetBytes(_parameter.ABC);
                c.Response.ContentType = "text/html; charset=utf-8";
                c.Response.WriteAsync(msg);
                //处理操作
                return Next.Invoke(c);
            }
            return Task.FromResult<int>(0);
        }
  public class CustomMiddlewareParameters {

        public string ABC { get; set; }

    }
    public static class CustomMiddlewareExtentions
    {
        public static IAppBuilder UseLYMMiddleware(this IAppBuilder builder)
        {
            return builder.Use<CustomMiddleware>(new CustomMiddlewareParameters { ABC="这是测试"});

        }
    

}
public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
          
            app.UseLYMMiddleware();
            app.Run(async context => await context.Response.WriteAsync("Hello World!"));

            // 有关如何配置应用程序的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkID=316888
        }
    }

 

posted @ 2017-11-15 17:47  龙码精神  阅读(392)  评论(0编辑  收藏  举报