是否有一个等同于"HttpContext.Response.Write"的消息?在Asp.Net Core 2中? [英] Is there an equivalent to "HttpContext.Response.Write" in Asp.Net Core 2?
本文介绍了是否有一个等同于"HttpContext.Response.Write"的消息?在Asp.Net Core 2中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Asp.Net Core 2中的ActionFilter在页面上附加一些HTML和Javascript内容.
I'm trying to append some HTML and Javascript content on page using ActionFilter in Asp.Net Core 2.
在MVC中,它正在与
filterContext.HttpContext.Response.Write(stringBuilder.ToString());
但是在Core中它不起作用.
but in Core it not working.
我试图用这个来实现:
filterContext.HttpContext.Response.WriteAsync(stringBuilder.ToString());
但是它会使整个页面空白.
But it make complete page to blank.
我正在寻找内置于Asp.Core 2.0中的nopCommerce 4.0的解决方案
I'm looking solution for nopCommerce 4.0 which build in Asp.Core 2.0
推荐答案
您可以尝试这样的事情
在 INopStartup.Configure(IApplicationBuilder应用程序)
application.Use(async (context, next) =>
{
using (var customStream = new MemoryStream())
{
// Create a backup of the original response stream
var backup = context.Response.Body;
// Assign readable/writeable stream
context.Response.Body = customStream;
await next();
// Restore the response stream
context.Response.Body = backup;
// Move to start and read response content
customStream.Seek(0, SeekOrigin.Begin);
var content = new StreamReader(customStream).ReadToEnd();
// Write custom content to response
await context.Response.WriteAsync(content);
}
});
并且比您的自定义 ResultFilterAttribute
public class MyAttribute : ResultFilterAttribute
{
public override void OnResultExecuted(ResultExecutedContext context)
{
try
{
var bytes = Encoding.UTF8.GetBytes("Foo Bar");
// Seek to end
context.HttpContext.Response.Body.Seek(context.HttpContext.Response.Body.Length, SeekOrigin.Begin);
context.HttpContext.Response.Body.Write(bytes, 0, bytes.Length);
}
catch
{
// ignored
}
base.OnResultExecuted(context);
}
}
结果
希望这有助于进入正确的方式.
Hope this helps to get into the right way.
这篇关于是否有一个等同于"HttpContext.Response.Write"的消息?在Asp.Net Core 2中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文