Cómo agregar videos de YouTube a tu página web con ASP.NET

Para todos aquellos que necesitan agregar videos en su página web, en este post, voy a explicar cómo se puede agregar videos de YouTube en tu sitio web con ASP.NET. Lo primero que tienes que hacer es buscar un video en YouTube que quieras poner en tu página y copias la URL. En mi caso usare la siguiente url https://www.youtube.com/watch?v=MZtB7pQFAV4
El código html de la página es el siguiente
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="AgregarVideoAspNet.aspx.vb" Inherits="AgregarVideoAspNet" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div id="divVideo" runat="server"> </div> </form> </body> </html>
En la página, cree un control html Div y hay que darle un valor en el id y hacer que corra en el servidor con lo siguiente runat="server"
Ahora en el código vb escribiremos la siguiente función y la puedes llamar del lugar que tú gustes. En mi caso yo la llamo en el evento Page_Load.
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load 'YouTube Video URL cargarVideo() End Sub Protected Sub cargarVideo() Dim youtubeUrl As String = "https://www.youtube.com/watch?v=MZtB7pQFAV4" Dim vCode As String = youtubeUrl.Substring(youtubeUrl.LastIndexOf("v=") + 2) If (vCode.Contains("&")) Then vCode = vCode.Substring(0, vCode.LastIndexOf("&")) End If Dim sHtml As String = "<object width='{0}' height='{1}' " & _ "data='http://www.youtube.com/v/{2}&autoplay=0' codetype='application/x-shockwave-flash'>" & _ "<param name='movie' value='http://www.youtube.com/v/{2}&autoplay=0'></param></object>" 'define frame size Dim sWidth As String = "640px" Dim sHeight As String = "300px" 'insert code to the Div divVideo.InnerHtml = String.Format(sHtml, sWidth, sHeight, vCode) End Sub
Espero que este artículo te sea de utilidad. Me gustaría que pudieras dejarme un comentario acerca del post. Tu información es muy valiosa para mí, si quieres preguntar o comentar acerca de este artículo, todo es bienvenido.