Skip to content
Learn Netverks
0

C# WebView2 and viewing local HTML files with YouTube embedded in <iframe>

asked 9 hours ago by @qa-ffjhsoflhivvx8ljpgzf 0 rep · 83 views

c# iframe youtube webview2 liveserver

I asked this question about viewing local HTML files with YouTube embedded in <iframe> in my browser.

Local HTML file embed youtube with <iframe>

It was answered that it couldn't be done the best way was to use Visual Studio Code and Live Server. That worked great however if I want my family or friends want to view HTML files with YouTube embedded in <iframe> then I cant really expect them to install Visual Studio Code and Live Server. I am in the process of learning C# as well and I have created a few small apps. So I created an app with a WebView2 control and set the Source property to http://127.0.0.1:5500/testvideo.html

this is the testvideo.html file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Local Video Embed Test</title>
</head>
<body>
    <h1>My Local Web Page</h1>
    
    <!-- Paste your copied YouTube iframe code here -->
    <iframe width="560" height="315" 
            src="https://www.youtube.com/embed/1IxxwvR6qSA?si=GjL_9UnP4EtQJTHs" 
            allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" 
            referrerpolicy="strict-origin-when-cross-origin" 
            allowfullscreen>
    </iframe>

</body>
</html>

When I run the Visual Studio Code and Live Server my C# app displays the Youtube iframe as expected in the WebView2 control. Obviously when I stop the Live Server the page cannot be displayed. My question is you guessed it, is there a way to add the Live Server functionality to my C# app without using Visual Studio Code and Live Server?

Comments on this question (0)

Use comments to ask for clarification — answers go in the answer box below.

Log in to comment on this question.

0

Just don't navigate to the file Uri. You can usually bypass the file/// resource or local origin issue when navigating to the raw file content instead of navigating to the file as a local resource.

Either navigate to the file content/raw HTML or create a virtual host mapping. See my answer for the details.

Blake Lopez · 0 rep · 9 hours ago

Your answer