简体   繁体   English

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

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

I have a simple question, and I hope any one of you could help me out. 我有一个简单的问题,我希望你们中的任何一个人都可以帮助我。 My question is as follows: 我的问题如下:

I have defined some properties to run some videos in an HTML5 player [named Flowplayer], and the properties that am using are: 我已经定义了一些属性来在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) Now here is the code which I really need your help in: 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);

Well, the above code doesn't work. 好吧,上面的代码不起作用。 I am pretty sure that the aforementioned text in the code is wrong (Especially everything inside the <video>...</video> ) and needs to be fixed. 我很确定代码中的上述文本是错误的(特别是<video>...</video> )并且需要修复。 So, Could you please help me in it ? 那么,请你帮帮我吧?

b) Here is another way I used to use: 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);

This second code way works fine, But this is quite impractical because it ONLY works with 1 single video format (video/webm) and 1 single video file (Hardcoded). 第二种代码方式工作正常,但这是不切实际的,因为它只适用于1种单一视频格式(视频/ webm)和1种单一视频文件(硬编码)。 I really want it to be more flexible and be able to take the values of both videoformat and videosource from the variables. 我真的希望它更灵活,能够从变量中获取视频格式和视频源的值。

So, My goal is to resolve point ( a ) and embed the C# code properly into the HTML tags. 因此,我的目标是解决( a )点并将C#代码正确嵌入到HTML标记中。

Thanks a lot ! 非常感谢 !

Here's your original snippet 1: 这是您的原始代码段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);

Here is your snippet 1 reformatted to make it readable: 这是您的代码段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);

This makes the mistake more obvious: you're referencing argument 0 more than once, and only used three of the values you need. 这使得错误更加明显:您不止一次引用参数0,并且只使用了您需要的三个值。 Do this instead: 改为:

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);

Also, don't forget to assign the result somewhere. 另外,不要忘记将结果分配到某处。 Finally, to condense it back to match the original: 最后,将其缩回以匹配原始:

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);

Does this work for you? 这对你有用吗?

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);

I just updated your string with substitution markers 0-3 and then matched up the arguments. 我刚刚用替换标记0-3更新了你的字符串,然后匹配了参数。

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

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