繁体   English   中英

如何将这个C#代码嵌入到HTML中?

[英]How to embed this C# code into HTML?

我有一个简单的问题,我希望你们中的任何一个人都可以帮助我。 我的问题如下:

我已经定义了一些属性来在HTML5播放器[名为Flowplayer]中运行一些视频,并且正在使用的属性是:

public string VideoSource; //it values are the source path of the video files
public string videoformat; //it values are e.g. video/webm and video/mp4
public int width; // The width of the player 
public int height; // The height of the player

a)现在这里是我真正需要你帮助的代码:

string.Format("<div style=\"width:{0}px;height:{1}px\" id='video1' class='flowplayer' data-swf='flowplayer.swf' data-ratio='0.4167'><video><source type='videoformat:{0}' src='VideoSource:{0}'/></video></div>", videoformat, width, height);

好吧,上面的代码不起作用。 我很确定代码中的上述文本是错误的(特别是<video>...</video> )并且需要修复。 那么,请你帮帮我吧?

b)这是我以前使用的另一种方式:

string.Format("<div style=\"width:{0}px;height:{1}px\" id='video1' class='flowplayer' data-swf='flowplayer.swf' data-ratio='0.4167'><video><source type='video/webm' src='http://myepiserversite/Global/WebmFileSample.webm' /></video></div>", videoformat, width, height);

第二种代码方式工作正常,但这是不切实际的,因为它只适用于1种单一视频格式(视频/ webm)和1种单一视频文件(硬编码)。 我真的希望它更灵活,能够从变量中获取视频格式和视频源的值。

因此,我的目标是解决( a )点并将C#代码正确嵌入到HTML标记中。

非常感谢 !

这是您的原始代码段1:

string.Format("<div style=\"width:{0}px;height:{1}px\" id='video1' class='flowplayer' data-swf='flowplayer.swf' data-ratio='0.4167'><video><source type='videoformat:{0}' src='VideoSource:{0}'/></video></div>", videoformat, width, height);

这是您的代码段1重新格式化以使其可读:

string.Format(
 @"<div style=""width:{0}px;height:{1}px"" id='video1' class='flowplayer' data-swf='flowplayer.swf' data-ratio='0.4167'>
     <video>
        <source type='videoformat:{0}' src='VideoSource:{0}'/>
   </video></div>", 
   videoformat, width, height);

这使得错误更加明显:您不止一次引用参数0,并且只使用了您需要的三个值。 改为:

string.Format(
 @"<div style=""width:{0}px;height:{1}px"" id='video1' class='flowplayer' data-swf='flowplayer.swf' data-ratio='0.4167'>
     <video>
        <source type='videoformat:{2}' src='VideoSource:{3}'/>
   </video></div>", 
   width, height, videoformat, VideoSource);

另外,不要忘记将结果分配到某处。 最后,将其缩回以匹配原始:

string result = string.Format("<div style=\"width:{0}px;height:{1}px\" id='video1' class='flowplayer' data-swf='flowplayer.swf' data-ratio='0.4167'><video><source type='videoformat:{2}' src='VideoSource:{3}'/></video></div>", width, height, videoformat, VideoSource);

这对你有用吗?

string.Format("<div style=\"width:{0}px;height:{1}px\" id='video1' class='flowplayer' data-swf='flowplayer.swf' data-ratio='0.4167'>**<video><source type='videoformat:{2}' src='VideoSource:{3}'/></video>**</div>", width, height, videoFormat, VideoSource);

我刚刚用替换标记0-3更新了你的字符串,然后匹配了参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM