jsm*_*ars 4 c# blazor blazor-server-side
<iframe>在我的 Blazor 页面上使用容器的标准 youtube 嵌入视频无法正确加载。什么都没有加载。
<iframe width='560' height='315' src='https://www.youtube.com/embed/m8e-FF8MsqU' frameborder='0' allow='accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture' allowfullscreen></iframe>
Run Code Online (Sandbox Code Playgroud)
在我的页面中,链接实际上是与@video链接链接的,但结果是相同的。
<iframe width='560' height='315' src='https://www.youtube.com/embed/@video' frameborder='0' allow='accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture' allowfullscreen></iframe>
Run Code Online (Sandbox Code Playgroud)
查看源代码,<html>在#document.
<html><head></head><body></body><link type="text/css" id="dark-mode" rel="stylesheet"><style type="text/css" id="dark-mode-custom-style"></style></html>
Run Code Online (Sandbox Code Playgroud)
而在普通的 HTML 页面中,结构充满了来自 Youtube 的内容。(缩短)
<!DOCTYPE html> <html lang="en" dir="ltr" data-cast-api-enabled="true">
<head><meta name="viewport" content="width=device-width, initial-scale=1"><style name="www-roboto" >@font-face{font-family:'Roboto';font-style:italic;font-weight:500;src:local('Roboto Medium Italic'),local('Roboto-MediumItalic'),url(//fonts.gstatic.com/s/roboto/v18/KFOjCnqEu92Fr1Mu51S7ACc3CsTKlA.woff2)format('woff2');unicode-range:U+0460-052F,U+1C80-1C88,U+20B4,U+2DE0-2DFF,U+A640-A69F,U+FE2E-FE2F;}
Run Code Online (Sandbox Code Playgroud)
如何正确嵌入 以便它在 Blazor 中按预期工作?
我已经测试了 YTiframe并且它在两者blazorserver和blazorwasm(Net Core 3 预览版 9)上运行:
@page "/counter"
<h1>Counter</h1>
<p>Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@if (currentCount % 2 == 0)
{
<iframe width="560" height="315" src="https://www.youtube.com/embed/oJYGSy6fRic" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
}
else
{
<iframe width="560" height="315" src="https://www.youtube.com/embed/m8e-FF8MsqU" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
}
@code {
int currentCount = 0;
void IncrementCount()
{
currentCount++;
}
}
Run Code Online (Sandbox Code Playgroud)
注意:我只为了测试目的而放入iframe了一个if,它也可以在没有if.
感谢 Dani,我确认通常这实际上在 blazor 中有效,但在我的具体情况下,问题是我使用的是单引号而不是双引号。更改代码使其工作。不确定这是否是 blazor 或 chrome for iframe 中的错误,还是预期的功能。如果有人有这方面的信息,我很乐意知道原因。
另外,我发现此行为的另一个原因是在嵌入页面的 url 中使用HTTP而不是。HTTPS它在 PHP 中甚至在 youtube 上都可以正常工作,但在 blazor 中似乎不起作用。
<iframe width="480" height="270" src="https://www.youtube.com/embed/@video" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
Run Code Online (Sandbox Code Playgroud)