Ska*_*osh 5 asp.net-core-webapi asp.net-core-2.0
在asp.net核心web api中间件中,是否可以访问中间件内的配置?有人可以指导我如何做到这一点?
在middle-ware您可以访问设置.要做到这一点,你需要得到IOptions<AppSettings>的middle-ware构造.见以下样本.
public static class HelloWorldMiddlewareExtensions
{
public static IApplicationBuilder UseHelloWorld(
this IApplicationBuilder builder)
{
return builder.UseMiddleware<HelloWorldMiddleware>();
}
}
public class HelloWorldMiddleware
{
private readonly RequestDelegate _next;
private readonly AppSettings _settings;
public HelloWorldMiddleware(
RequestDelegate next,
IOptions<AppSettings> options)
{
_next = next;
_settings = options.Value;
}
public async Task Invoke(HttpContext context)
{
await context.Response.WriteAsync($"PropA: {_settings.PropA}");
}
}
public class AppSettings
{
public string PropA { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
欲了解更多信息,请点击这里.
| 归档时间: |
|
| 查看次数: |
1811 次 |
| 最近记录: |