[英]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");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.