如何在blazor页面显示api返回的text/html?
How to display the text/html returned by the api on the blazor page?
@page "/counter"
<p>Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@if (currentCount % 2 == 0)
{
<iframe width="1660" height="1115" src="https://www.youtube.com/embed/m8e-FF8MsqU" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
}
else
{
<iframe width="1660" height="1115" src="http://localhost:10726/api/Employee/HtmlReport" frameborder="0" allowfullscreen></iframe>
}
@code {
int currentCount = 0;
void IncrementCount()
{
currentCount++;
}
}
下面是返回的text/html:
enter image description here
但是blazor页面无法显示,但会下载。怎么做:
enter image description here
我这里使用的代码:
enter link description here
谢谢!
正如@Henk Holterman 所说,问题出在您的后端代码中。这是一个工作演示:
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET: api/<ValuesController>
[HttpGet]
public async Task<IActionResult> Get()
{
string filePath = "test.pdf"; //test.pdf located in wwwroot folder
var contentDisposition = new System.Net.Mime.ContentDisposition
{
FileName = "test.pdf",
Inline = true
};
Response.Headers.Add("Content-Disposition", contentDisposition.ToString());
return File(filePath, System.Net.Mime.MediaTypeNames.Application.Pdf);
}
}
Index.razor:
<iframe width="1660" height="1115" src="https://localhost:44304/api/values" frameborder="0" allowfullscreen></iframe>
@page "/counter"
<p>Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@if (currentCount % 2 == 0)
{
<iframe width="1660" height="1115" src="https://www.youtube.com/embed/m8e-FF8MsqU" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
}
else
{
<iframe width="1660" height="1115" src="http://localhost:10726/api/Employee/HtmlReport" frameborder="0" allowfullscreen></iframe>
}
@code {
int currentCount = 0;
void IncrementCount()
{
currentCount++;
}
}
下面是返回的text/html: enter image description here
但是blazor页面无法显示,但会下载。怎么做: enter image description here
我这里使用的代码: enter link description here
谢谢!
正如@Henk Holterman 所说,问题出在您的后端代码中。这是一个工作演示:
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET: api/<ValuesController>
[HttpGet]
public async Task<IActionResult> Get()
{
string filePath = "test.pdf"; //test.pdf located in wwwroot folder
var contentDisposition = new System.Net.Mime.ContentDisposition
{
FileName = "test.pdf",
Inline = true
};
Response.Headers.Add("Content-Disposition", contentDisposition.ToString());
return File(filePath, System.Net.Mime.MediaTypeNames.Application.Pdf);
}
}
Index.razor:
<iframe width="1660" height="1115" src="https://localhost:44304/api/values" frameborder="0" allowfullscreen></iframe>