是否有等效于“HttpContext.Response.Write"?在 Asp.Net Core 2 中?

编程入门 行业动态 更新时间:2024-10-07 06:43:57
本文介绍了是否有等效于“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 中?

本文发布于:2023-11-03 04:00:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554186.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Write   Response   HttpContext   quot   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!