(精华)2020年9月17日 ASP.NET Core 中间件详解
【摘要】
1.基本中间件的使用
public static void Start()
{
var host = Host.CreateDefaultBuilder()
.Configure...
1.基本中间件的使用
public static void Start()
{
var host = Host.CreateDefaultBuilder()
.ConfigureWebHost(builder => builder
.UseKestrel()
.Configure(app =>
app.Run(context =>
context.Response.WriteAsync("Hello!"))
)
).Build();
host.Run();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
public static class Sample02
{
public static void Start()
{
var host = Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(builder => builder
.Configure(app => app
.Use(Middleware1)
.Use(Middleware2)
)
).Build();
host.Run();
}
private static RequestDelegate Middleware1(RequestDelegate next)
{
async Task App(HttpContext context)
{
await context.Response.WriteAsync("Middleware 1 Begin.");
await next(context);
await context.Response.WriteAsync("Middleware 1 End.");
}
return App;
}
private static RequestDelegate Middleware2(RequestDelegate next)
{
async Task App(HttpContext context)
{
await context.Response.WriteAsync("Middleware 2 Begin.");
}
return App;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
2.强类型中间件的使用
public class Sample03
{
public class HelloMiddleware : IMiddleware
{
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
await context.Response.WriteAsync("Hello");
}
}
public static void Start()
{
var host = Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(builder => builder
.ConfigureServices(collection => collection.AddSingleton<HelloMiddleware>())
.Configure(app => app
.UseMiddleware<HelloMiddleware>()
)
).Build();
host.Run();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
3.约定中间件的使用
public class Sample04
{
public class HelloMiddleware
{
private readonly RequestDelegate _next;
private readonly string _content;
private readonly bool _isToNext;
public HelloMiddleware(RequestDelegate next, string content, bool isToNext = false)
{
_next = next;
_content = content;
_isToNext = isToNext;
}
public async Task InvokeAsync(HttpContext httpContext)
{
await httpContext.Response.WriteAsync($"Hello {_content}!\r\n");
if (_isToNext) await _next(httpContext);
}
}
public static void Start()
{
var host = Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(builder => builder
.Configure(app => app
.UseMiddleware<HelloMiddleware>("Rick", true)
.UseMiddleware<HelloMiddleware>("Motry")
)
).Build();
host.Run();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
4.Startup的由来和使用
public static class Sample06
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
foreach (var service in services)
{
var serviceName = service.ServiceType.Name;
var implType = service.ImplementationType;
if (implType != null)
{
Console.WriteLine($"{service.Lifetime, -15}{serviceName,-40}{implType.Name}");
}
}
}
public void Configure(IApplicationBuilder app)
{
}
}
public static void Start()
{
var host = Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(builder => builder
.UseStartup<Startup>())
.Build();
host.Run();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
public class Sample07
{
public class Startup
{
public Startup(IConfiguration configuration, IHostEnvironment hostingEnvironment)
{
Debug.Assert(configuration != null);
Debug.Assert(hostingEnvironment != null);
}
public void ConfigureServices(IServiceCollection services)
{
}
public void Configure(IApplicationBuilder app)
{
}
}
public static void Start()
{
var host = Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(builder => builder
.UseStartup<Startup>())
.Build();
host.Run();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
public class Sample09
{
public class Test1{ };
public class Test2{ };
public class TestMiddleware
{
private readonly RequestDelegate _next;
public TestMiddleware(RequestDelegate next, Test1 test1, Test2 test2)
{
Debug.Assert(test1 != null);
Debug.Assert(test2 != null);
_next = next;
}
public async Task InvokeAsync(HttpContext httpContext)
{
Debug.Assert(_next != null);
await httpContext.Response.WriteAsync("Test");
}
}
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services
.AddSingleton<Test1>()
.AddSingleton<Test2>();
}
public void Configure(IApplicationBuilder app)
{
app.UseMiddleware<TestMiddleware>();
}
}
public static void Start()
{
var host = Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(builder => builder
.UseStartup<Startup>())
.Build();
host.Run();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
public class Sample10
{
public class Test1{ };
public class Test2{ };
public class TestMiddleware
{
private readonly RequestDelegate _next;
public TestMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext httpContext,Test1 test1, Test2 test2)
{
Debug.Assert(test1 != null);
Debug.Assert(test2 != null);
await httpContext.Response.WriteAsync("Test");
}
}
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services
.AddSingleton<Test1>()
.AddSingleton<Test2>();
}
public void Configure(IApplicationBuilder app)
{
app.UseMiddleware<TestMiddleware>();
}
}
public static void Start()
{
var host = Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(builder => builder
.UseStartup<Startup>())
.Build();
host.Run();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
public class Sample11
{
public class Test1{ }
public class Test2{ }
public static void Start()
{
var host = Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(builder => builder
.ConfigureServices(collection => collection
.AddSingleton<Test1>()
.AddSingleton<Test2>()
.AddControllersWithViews())
.Configure(app=>app
.UseRouting()
.UseEndpoints(routeBuilder => routeBuilder.MapControllers()))
)
.Build();
host.Run();
}
}
public class Startup
{
public void Configure(IApplicationBuilder app)
{
}
public void ConfigureServices(IServiceCollection services)
{
}
}
public class HomeController : Controller
{
private readonly Sample11.Test1 _test1;
public HomeController(Sample11.Test1 test1)
{
_test1 = test1;
}
[HttpGet("/")]
public IActionResult Index()
{
ViewBag.Test1 = _test1;
return View();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
文章来源: codeboy.blog.csdn.net,作者:愚公搬代码,版权归原作者所有,如需转载,请联系作者。
原文链接:codeboy.blog.csdn.net/article/details/108524678
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)