C# 在ASP.NET web应用程序中创建短代码
我正在一个网站上工作,那里会嵌入很多YouTube视频。我想让它们更容易嵌入到页面的文章中 写这篇文章时:C# 在ASP.NET web应用程序中创建短代码,c#,asp.net,iframe,youtube,shortcode,C#,Asp.net,Iframe,Youtube,Shortcode,我正在一个网站上工作,那里会嵌入很多YouTube视频。我想让它们更容易嵌入到页面的文章中 写这篇文章时: [youtube]SOMETHING[/youtube] 页面应自动创建以下内容: <iframe src="//www.youtube.com/embed/SOMETHING" frameborder="0" allowfullscreen></iframe> 那我该怎么做呢?我一直在四处寻找,但没有找到正确的解决方案。请在ASP
[youtube]SOMETHING[/youtube]
页面应自动创建以下内容:
<iframe src="//www.youtube.com/embed/SOMETHING"
frameborder="0" allowfullscreen></iframe>
那我该怎么做呢?我一直在四处寻找,但没有找到正确的解决方案。请在ASP.NET/C#中提供示例。在ASP.NET中创建短代码作为自定义解决方案很容易。在输出文章之前,请执行以下操作:
String html = "[YOUTUBE]Something[\\YOUTUBE]";
String replacementHtml = "<iframe src=\"//www.youtube.com/embed/$1\" frameborder=\"0\" allowfullscreen></iframe>";
Regex shortcodeRegex = new Regex(@"\[YOUTUBE\]([^\[\\]+)\[\\YOUTUBE\]");
String result = shortcodeRegex.Replace(html, replacementHtml);
String html=“[YOUTUBE]某物[\\YOUTUBE]”;
字符串替换HTML=“”;
正则表达式shortcodeRegex=新正则表达式(@“\[YOUTUBE\]”([^\[\\]+)\[\\YOUTUBE\]”);
字符串结果=shortcodeRegex.Replace(html,replacementHtml);
注意replacementHtml中的$1
。这是由匹配的内部内容替换的内容
然后将结果输出到页面。这就是我用来处理youtube短代码的方法
<Extension> Public Function ConvertYouTubeShortCode(text As String) As String
Dim regex__1 As String = "\[youtube:.*?\]"
Dim matches As MatchCollection = Regex.Matches(text, regex__1)
If matches.Count = 0 Then
Return text
End If
Dim width As Int32 = 620
Dim height As Int32 = 349
Dim BaseURL As String = "http://www.youtube.com/v/"
For i As Integer = 0 To matches.Count - 1
Dim length As Int32 = "[youtube:".Length
Dim mediaFile As String = matches(i).Value.Substring(length, matches(i).Value.Length - length - 1)
Dim player As String = "<div class=""video-container""><iframe width=""{2}"" height=""{3}"" src=""{4}{1}"" frameborder=""0"" allowfullscreen></iframe></div>"
Return text.Replace(matches(i).Value, [String].Format(player, i, mediaFile, width, height, BaseURL))
Next
End Function
public partial class page : System.Web.UI.Page
{
protected void Page_Init(object sender, EventArgs e)
{
string txt="<div>blala [slide_plugins /] blabla</div>";
icerikLtrl.Text = modul_islemler.modul_olustur(txt);
}
公共函数将TubeShortCode(文本作为字符串)转换为字符串
Dim regex\uu 1作为字符串=“\[youtube:.*?\]”
将匹配项设置为MatchCollection=Regex.matches(文本,Regex\uu 1)
如果匹配。计数=0,则
返回文本
如果结束
尺寸宽度为Int32=620
变暗高度为Int32=349
Dim BaseURL作为字符串=”http://www.youtube.com/v/"
对于i As Integer=0匹配。计数-1
变暗长度为Int32=“[youtube:”.length
Dim mediaFile As String=matches(i).Value.Substring(长度,matches(i).Value.length-length-length-1)
将播放器变暗为String=“”
返回text.Replace(匹配(i).Value[String].格式(播放器、i、媒体文件、宽度、高度、BaseURL))
下一个
端函数
Wordpress样式的短代码应用程序。它用用户控件内容替换来自数据库的内容或变量中变量的值[short code/]
应用程序代码-->模块islemler.cs
public class modul_islemler
{
public static string modul_olustur(string data){
string aranan = @"\[(.*?)\/\]";
Regex objRegex = new Regex(aranan);
MatchCollection objCol = objRegex.Matches(data);
foreach (Match item in objCol)
{data = data.Replace(item.Groups[0].Value, modul_yaz(item.Groups[1].Value.ToString()));
}
return data;
}
public static string modul_yaz(string sayfa)
{
string[] ayir = sayfa.Split(' ');
ArrayList myAL = new ArrayList();
foreach (string a in ayir)
{
myAL.Add(a);
}
if (myAL.Count < 2) myAL.Add("");
return LoadControl("~/plugins/" + myAL[0] + "/" + myAL[0] + ".ascx");
}
public static string LoadControl(string UserControlPath)
{
FormlessPage page = new FormlessPage();
page.EnableViewState = false;
// Create instance of the user control
UserControl userControl = (UserControl)page.LoadControl(UserControlPath);
page.Controls.Add(userControl);
//Write the control Html to text writer
StringWriter textWriter = new StringWriter();
//execute page on server
HttpContext.Current.Server.Execute(page, textWriter, false);
// Clean up code and return html
return textWriter.ToString();
}
public class FormlessPage : Page
{
public override void VerifyRenderingInServerForm(Control control)
{
}
}
ASP.Net与WebForms、ASP.Net MVC、自定义框架?ASPX或Razor(CSHTML/VBHTML)?当您在VS或页面上的某些处理区域或来自外部源(如DB)的某些数据中编辑时,您希望“编写此文件时”在何处展开?它是否与Wordpress(基于标记)有某种关联?谢谢。你能告诉我,我如何在全局范围内使用它吗?如果我想在多个页面上使用相同的代码,我不想在所有页面上都使用相同的代码。当你从数据库或文件中获取html时,它很可能都来自类库中的相同代码。只需在返回页面之前对其进行编辑。请吃了你的答案代码,只有答案的帮助不大。
public partial class page : System.Web.UI.Page
{
protected void Page_Init(object sender, EventArgs e)
{
string txt="<div>blala [slide_plugins /] blabla</div>";
icerikLtrl.Text = modul_islemler.modul_olustur(txt);
}
<asp:TextBox runat="server" ID="Txt1"></asp:TextBox>
<asp:Button runat="server" ID="btn1" OnClick="btn1_Click" Text="Submit"></asp:Button>
protected override void OnLoad(EventArgs e)
{
//kontrol yüklendiğinde çalışacak kodlar
base.OnLoad(e);
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
InitializeComponent();
}
private void InitializeComponent()
{
btn1.Click += new EventHandler(btn1_Click);
}
protected void btn1_Click(object sender, EventArgs e)// not working....
{
Txt1.Text = "Example"; // not working....
}