ASP.NET Core 2.0中间件

编程入门 行业动态 更新时间:2024-10-27 13:30:05
本文介绍了ASP.NET Core 2.0中间件-访问配置设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在asp核心Web API中间件中,是否可以访问中间件中的配置?有人可以指导我如何做到吗?

In a asp core web api middleware, is it possible to access the configuration inside the middleware? Can someone guide me how this is done?

推荐答案

在middle-ware中,您可以访问设置.为此,您需要在middle-ware构造函数中获取IOptions<AppSettings>.请参阅以下示例.

In a middle-ware you can access settings. To achieve this, you need to get IOptions<AppSettings> in the middle-ware constructor. See following sample.

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

有关更多信息,请参见此处.

For more information see here.

更多推荐

ASP.NET Core 2.0中间件

本文发布于:2023-11-15 07:55:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1592573.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中间件   NET   ASP   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!