将参数传递给ASP.NET Core 2.0中的中间件
问题
在ASP.NET Core的安装过程中,如何将参数传递给中间件?
解
在一个空的项目中添加一个POCO类来保存中间件的参数,
-
publicclass GreetingOptions
-
{
-
public string GreetAt { get; set; }
-
public string GreetTo { get; set; }
-
}
添加一个中间件,
-
publicclass GreetingMiddleware
-
{
-
private readonly RequestDelegate next;
-
private readonly GreetingOptions options;
-
public GreetingMiddleware(
-
RequestDelegate next,
-
GreetingOptions options)
-
{
-
this.next = next;
-
this.options = options;
-
}
-
public async Task Invoke(
-
HttpContext context)
-
{
-
var message = $"Good {this.options.GreetAt} {this.options.GreetTo}";
-
await context.Response.WriteAsync(message);
-
}
-
}
解决方案A - 实例类型
添加扩展方法来配置中间件,
-
publicstatic IApplicationBuilder UseGreeting(
-
this IApplicationBuilder app, GreetingOptions options)
-
{
-
return app.UseMiddleware<GreetingMiddleware>(options);
-
}
配置中间件,
-
publicvoid Configure(
-
IApplicationBuilder app,
-
IHostingEnvironment env)
-
{
-
app.UseGreeting(new GreetingOptions
-
{
-
GreetAt = "Morning",
-
GreetTo = "Tahir"
-
});
-
}
解决方案B - 功能类型
添加扩展方法来配置中间件,
-
publicstatic IApplicationBuilder UseGreeting(
-
this IApplicationBuilder app, Action<GreetingOptions> configureOptions)
-
{
-
var options = new GreetingOptions();
-
configureOptions(options);
-
return app.UseMiddleware<GreetingMiddleware>(options);
-
}
配置中间件,
-
publicvoid Configure(
-
IApplicationBuilder app,
-
IHostingEnvironment env)
-
{
-
app.UseGreeting(options =>
-
{
-
options.GreetAt = "Morning";
-
options.GreetTo = "Tahir";
-
});
-
}
讨论
我在之前的文章 中讨论过, 在独立的类中定义中间件并使用扩展方法添加到管道中是一种好的做法 。虽然我们也可能需要将信息传递给我们的中间件类,但是在深入挖掘ASP.NET Core源代码和其他样本时,我已经遇到了两种模式。
如上面的解决方案A和B所证明的那样,这是非常简单的。我们将我们的参数包装在一个POCO类中,并创建一个扩展方法,
-
POCO实例
-
函数调用,进而建立POCO。
请注意
POCO传递给构造函数中的中间件。UseMiddleware()方法需要params对象[]参数传递到中间件构造函数。
配置服务
这些模式也可以用来设置 服务容器的依赖注入。为了演示添加服务,
-
publicclass MessageService : IMessageService
-
{
-
private readonly MessageOptions options;
-
public MessageService(MessageOptions options)
-
{
-
this.options = options;
-
}
-
public string FormatMessage(string message)
-
{
-
// use options
-
returnthis.options.Format == MessageFormat.None ? message :
-
this.options.Format == MessageFormat.Upper ? message.ToUpper() :
-
message.ToLower();
-
}
-
}
添加这些扩展方法来配置服务,
-
// Instance Type
-
publicstatic IServiceCollection AddMessageFormatter(
-
this IServiceCollection services, MessageOptions options)
-
{
-
return services.AddScoped<IMessageService>(factory =>
-
{
-
returnnew MessageService(options);
-
});
-
}
-
// Function Type
-
publicstatic IServiceCollection AddMessageFormatter(
-
this IServiceCollection services, Action<MessageOptions> configureOptions)
-
{
-
var options = new MessageOptions();
-
configureOptions(options);
-
return services.AddScoped<IMessageService>(factory =>
-
{
-
returnnew MessageService(options);
-
});
-
}
使用其中之一配置服务,
-
// Instance Type
-
publicvoid ConfigureServices(
-
IServiceCollection services)
-
{
-
services.AddMessageFormatter(new MessageOptions
-
{
-
Format = MessageFormat.Lower
-
});
-
}
-
// Function Type
-
publicvoid ConfigureServices(
-
IServiceCollection services)
-
{
-
services.AddMessageFormatter(options =>
-
{
-
options.Format = MessageFormat.Lower;
-
});
-
}
- 浅谈ASP.NET Core 2.0 带初始参数的中间件(译)
- [译]ASP.NET Core 2.0 带初始参数的中间件
- [译]ASP.NET Core 2.0 带初始参数的中间件
- [译]ASP.NET Core 2.0 带初始参数的中间件
- ASP.NET Core 2.0 带初始参数的中间件问题及解决方法
- 在Asp.net 2.0中用QueryString传递中文参数的解决方法!
- 在Asp.net 2.0中用QueryString传递中文参数的解决方法!
- 在Asp.net 2.0中用QueryString传递中文参数的解决方法!
- asp.net mvc 2.0页面带参数跳转以及获取传递的参数
- ASP.NET Boilerplate 学习 AspNet Core2 浏览器缓存使用 c#基础,单线程,跨线程访问和线程带参数 wpf 禁用启用webbroswer右键菜单 EF Core 2.0使用MsSql/MySql实现DB First和Code First ASP.NET Core部署到Windows IIS QRCode.js:使用 JavaScript 生成
- 【ASP.NET Core】给中间件传参数的方法
- 浅谈ASP.NET Core 2.0 中间件(译)
- ASP.NET Core 2.0系列学习笔记-Middleware中间件
- [译]ASP.NET Core 2.0 中间件
- [译]ASP.NET Core 2.0 中间件
- Asp.Net Core 2.0 项目实战(8)Core下缓存操作、序列化操作、JSON操作等Helper集合类
- asp.net中获取URL传递过来参数到代码
- ASP.NET传递中文参数乱码的解决方法
- asp.net core2.0中网站发布的时候,怎么样才配置才可以使视图文件不被打包进去?
- ASP.NET 页面间传递参数的方法(转)