【问题标题】:How to display the text/html returned by the api on the blazor page?如何在blazor页面显示api返回的text/html?
【发布时间】:2021-05-07 03:58:59
【问题描述】:
@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++;
    }
}

下面是返回的文本/html: enter image description here

但是,它不能显示在 blazor 页面上,但会被下载。怎么做: enter image description here

我这里使用的是代码: enter link description here

谢谢!

【问题讨论】:

  • youtube 链接可以正常工作吗?
  • 不要将相关代码放在外部链接或图片中。问题出在您的 API,而不是您的客户端。
  • 是的,youtube 链接工作正常

标签: c# asp.net-core blazor asp.net-core-webapi blazor-server-side


【解决方案1】:

正如@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>

【讨论】:

  • 感谢回复,测试你的代码就ok了,你看看我的api代码哪里出错了,谢谢:code link
  • 请先分享您的api代码,否则我该如何解决您的问题。
  • 我知道怎么做,没关系,就像你的代码一样。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多