gpt4 book ai didi

asp.net-core-2.0 - ASP.NET Core 2.0中间件-访问配置设置

转载 作者:行者123 更新时间:2023-12-03 23:38:31 25 4
gpt4 key购买 nike

在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; }
}

有关更多信息,请参见 here

关于asp.net-core-2.0 - ASP.NET Core 2.0中间件-访问配置设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47722937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com