簡體   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