我想在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();
喜@ademcaglin和感谢。我已经编写了一个OnResultExecuted过滤器,但我没有在我的htmlData处理过的剃刀html(我得到空字符串)。我如何编写一个捕获和更新由剃刀处理的html的动作过滤器?我不清楚过滤器在哪里执行,以及如何将我的地址放在可以捕获html并更新它的地方。我更新问题与“可能的但不工作的过滤器代码”。 –
你说得对。它似乎不适用于剃须刀输出。不过,我发现了一个中间件方法的解决方案,并按预期工作。查看我的更新。 –