简体   繁体   English

ASP.NET 核心 Razor 页面:将 html 内容写入新的浏览器选项卡?

[英]ASP.NET Core Razor Page: Write html content to new browser tab?

Using HttpClient , I make a GET request which returns a html response.使用HttpClient ,我发出一个返回 html 响应的 GET 请求。

Request to url in method handler:在方法处理程序中对 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 response content from request:来自请求的 Html 响应内容:

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

      <!-- body content -->

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

Attempting to add await Response.Body.WriteAsync(bytesResponse, 0, bytesResponse.Length);尝试添加await Response.Body.WriteAsync(bytesResponse, 0, bytesResponse.Length); will throw an System.ObjectDisposedException: IFeatureCollection has been disposed.将抛出System.ObjectDisposedException: IFeatureCollection has been disposed. exception例外

Would appreciate assistance on how to display the response in a new browser tab.希望获得有关如何在新的浏览器选项卡中显示响应的帮助。

Firstly,if you want to open a new browser tag,it cannot be done in razor handler.It need to be done in view.You need to decorate your link with the "_blank" attribute like this:首先,如果你想打开一个新的浏览器标签,它不能在 razor 处理程序中完成。它需要在视图中完成。你需要用“_blank”属性装饰你的链接,如下所示:

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

Here is a demo worked:这是一个演示:

test.cshtml:测试.cshtml:

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

test.cshtml.cs: test.cshtml.cs:

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

result:结果: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在asp.net核心剃须刀页面中重用HTML代码段? - How to reuse HTML snippets in asp.net core razor page? asp.NET Razor新页面错误 - asp.NET Razor new Page error 在 ASP.NET Core Razor 页面上提交 Post 后如何刷新页面内容? - How to refresh the page content after a Post submission on ASP.NET Core Razor Pages? ASP.NET Core Razor Page 为列表创建新页面总是给出 System.NullReferenceException - ASP.NET Core Razor Page create new for list always give System.NullReferenceException ASP.NET Core 2.1 Razor 页面返回带模型的页面 - ASP.NET Core 2.1 Razor page return Page with model 运行asp.net核心api项目时如何在vscode中设置不打开新的web浏览器选项卡 - How to set not to open new web browser tab in vscode when running asp.net core api project 使用 Asp.Net Core,如何将视图作为新的浏览器选项卡打开,然后稍后再关闭 - Using Asp.Net Core, how can I open a view as a new browser tab, and then close it again later 数据表 asp.net 核心剃刀页面 IActionResult 新 JsonResult - datatables asp.net core razor pages IActionResult new JsonResult ASP.NET Core Razor Page多路径路由 - ASP.NET Core Razor Page multiple path routing 如何在ASP.NET Core 2.0 Razor页面中填充dropdownlist - How to populate dropdownlist In ASP.NET Core 2.0 Razor page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM