本文介绍了是否有等效于“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)的自定义实现中
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 中?
发布评论