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

时间:2014-01-22 21:53:14

标签: c# html flowplayer

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

我已经定义了一些属性来在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标记中。

非常感谢!

2 个答案:

答案 0 :(得分:2)

这是您的原始代码段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);

答案 1 :(得分: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);

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