想要ASP.NET Core中间件在MVC Razor View Engine后运行
问题描述:
我想在MVC Razor View Engine处理完数据后在ASP.NET Core中运行MiddleWare模块。我可以让它运行,但它似乎没有收集所有的数据。我有一个标签助手来更新DI对象的集合,但是当中间件运行时,DI对象的集合是空的。我startup.cs看起来是这样的:想要ASP.NET Core中间件在MVC Razor View Engine后运行
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseMiddleware<MyMiddleware>();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
和我的中间件是这样的:
public class MyMiddleware
{
private readonly RequestDelegate nextMiddleware;
private readonly IScriptManager _scriptManager;
public MyMiddleware(RequestDelegate next, IScriptManager scriptManager)
{
this.nextMiddleware = next;
_scriptManager = scriptManager;
}
public async Task Invoke(HttpContext context)
{
var cnt = _scriptManager.ScriptTexts.Count;
.. get HTML
Stream originalStream = context.Response.Body;
...
.. update HTML
await context.Response.WriteAsync(htmlData);
我得到我想要的HTML,但它似乎在我的DI采集没有更新。
***注 - 可能的,但不是工作结果筛选
services.AddMvc(options =>
{
options.Filters.Add(new AppendToHtmlBodyFilter());
});
public class AppendToHtmlBodyFilter : TypeFilterAttribute
{
private readonly IScriptManager _scriptManager;
public AppendToHtmlBodyFilter():base(typeof(SampleActionFilterImpl))
{
}
private class SampleActionFilterImpl : IResultFilter
{
private readonly IScriptManager _scriptManager;
public SampleActionFilterImpl(IScriptManager scriptManager)
{
_scriptManager = scriptManager;
//_logger = loggerFactory.CreateLogger<SampleActionFilterAttribute>();
}
public void OnResultExecuted(ResultExecutedContext context)
{
var cnt = _scriptManager.ScriptTexts.Count;
Stream originalStream = context.HttpContext.Response.Body;
using (MemoryStream newStream = new MemoryStream())
{
context.HttpContext.Response.Body = newStream;
context.HttpContext.Response.Body = originalStream;
newStream.Seek(0, SeekOrigin.Begin);
StreamReader reader = new StreamReader(newStream);
var htmlData = reader.ReadToEnd();
答
据我所知是没有办法在请求管道MVC后运行一个中间件。如果你想操纵剃刀输出,你可以使用过滤器。结果过滤器似乎适合您的情况。
结果过滤器非常适合任何需要直接环绕 视图执行或格式化程序执行的逻辑。结果过滤器可以替换或修改负责生成响应的操作结果。
参见官方文档https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/filters#result-filters
也看到如何使用依赖注入过滤https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/filters#dependency-injection
更新
我无法得到它的结果过滤器的工作(它的工作JSON结果,但没有工作viewresult)。
但是我发现中间件一个很好的例子:http://www.mikesdotnetting.com/article/269/asp-net-5-middleware-or-where-has-my-httpmodule-gone
public class MyMiddleware
{
private readonly RequestDelegate nextMiddleware;
private readonly IScriptManager _scriptManager;
public MyMiddleware(RequestDelegate next, IScriptManager scriptManager)
{
this.nextMiddleware = next;
_scriptManager = scriptManager;
}
public async Task Invoke(HttpContext context)
{
var cnt = _scriptManager.ScriptTexts.Count;
using (var memoryStream = new MemoryStream())
{
var bodyStream = context.Response.Body;
context.Response.Body = memoryStream;
await _next(context);
var isHtml = context.Response.ContentType?.ToLower().Contains("text/html");
if (context.Response.StatusCode == 200 && isHtml.GetValueOrDefault())
{
memoryStream.Seek(0, SeekOrigin.Begin);
using (var streamReader = new StreamReader(memoryStream))
{
var responseBody = await streamReader.ReadToEndAsync();
// update html
using (var amendedBody = new MemoryStream())
using (var streamWriter = new StreamWriter(amendedBody))
{
streamWriter.Write(responseBody);
amendedBody.Seek(0, SeekOrigin.Begin);
await amendedBody.CopyToAsync(bodyStream);
}
}
}
}
}
}
喜@ademcaglin和感谢。我已经编写了一个OnResultExecuted过滤器,但我没有在我的htmlData处理过的剃刀html(我得到空字符串)。我如何编写一个捕获和更新由剃刀处理的html的动作过滤器?我不清楚过滤器在哪里执行,以及如何将我的地址放在可以捕获html并更新它的地方。我更新问题与“可能的但不工作的过滤器代码”。 –
你说得对。它似乎不适用于剃须刀输出。不过,我发现了一个中间件方法的解决方案,并按预期工作。查看我的更新。 –