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 } }
如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!
本文版权归作者和博客园共有,来源网址:http://www.cnblogs.com/liyouming欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接。