【问题标题】:ASP.NET Core Razor Page: Write html content to new browser tab?ASP.NET Core Razor 页面:将 html 内容写入新的浏览器选项卡?
【发布时间】:2020-11-26 15:14:34
【问题描述】:

使用HttpClient,我发出一个返回html响应的GET请求。

请求方法处理程序中的 url:

var response = string.Empty;
var result = await _httpClient.CreateClient().GetAsync(newUrl);
if (result.IsSuccessStatusCode)
{
  response = await result.Content.ReadAsStringAsync();
  var bytesResponse = Encoding.ASCII.GetBytes(response)

  // How to open bytesResponse in a new browser tab?

}

来自请求的 HTML 响应内容:

<!DOCTYPE html>
<html lang="en">
  <head>
    <style>
    </style>
  </head>
  <body>

      <!-- body content -->

    <script>
    </script>
  </body>
</html>

尝试添加await Response.Body.WriteAsync(bytesResponse, 0, bytesResponse.Length); 将引发System.ObjectDisposedException: IFeatureCollection has been disposed. 异常

非常感谢有关如何在新的浏览器标签中显示响应的帮助。

【问题讨论】:

    标签: c# html asp.net-core razor-pages


    【解决方案1】:

    首先,如果你想打开一个新的浏览器标签,它不能在 razor 处理程序中完成。它需要在视图中完成。你需要像这样用“_blank”属性装饰你的链接:

    <a asp-page-handler="xxxxx" target="_blank">link</a>
    

    这是一个演示:

    test.cshtml:

    <a asp-page-handler="Html" target="_blank">link</a>
    

    test.cshtml.cs:

    public IActionResult OnGet()
            {
                return Page();
            }
    public ActionResult OnGetHtml()
            {
                return base.Content("<div>Hello</div>", "text/html");
            }
    

    结果:

    【讨论】:

    • 非常感谢您的澄清(它不能在 razor 处理程序中完成)和页面流程。这让我找到了解决问题的正确方向。
    猜你喜欢
    • 2020-07-09
    • 1970-01-01
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    • 2015-06-11
    • 2016-03-21
    • 1970-01-01
    • 2021-12-17
    相关资源
    最近更新 更多