模版页中引用文件路径的问题[转]
时间:2011-02-26 来源:墟零
或者转换为服务端控件:<link rel="stylesheet" media="screen" type="text/css" href="~/css/global.css" runat="server"/>
当然如果你觉得每个路径都要写成动态的不爽,而又正好有页面基类的话,倒是可以换一种方式:
所有的路径直接书写为相对于应用程序目录的路径,形如:
<link rel="stylesheet" media="screen" type="text/css" href="~/css/global.css" />
当然默认HTML是不支持的这样的路径方式的,这时候就要借助PageBase了,代码如下(好像是从DNN的代码里面抠出来的):
public abstract class PageBase : Page { protected override void Render(HtmlTextWriter writer) { StringWriter stringWriter = new StringWriter(); HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter); base.Render(htmlWriter); string html = stringWriter.ToString(); #region 转换相对路径 MatchCollection collection = Regex.Matches(html, "<(a|link|img|script|input|form).[^>]*(href|src|action)=(\\\"|'|)(.[^\\\"']*)(\\\"|'|)[^>]*>", RegexOptions.IgnoreCase); foreach (Match match in collection) { if (match.Groups[match.Groups.Count - 2].Value.IndexOf("~") != -1) { string url = this.Page.ResolveUrl(match.Groups[match.Groups.Count - 2].Value); html = html.Replace(match.Groups[match.Groups.Count - 2].Value, url); } } #endregion writer.Write(html); } }
然后所有页面都继承PageBase即可,如果仅仅因为这个路径问题就动用到基类,貌似挺麻烦的!
相关阅读 更多 +