ASP.NET-主题和相关引用
我有一个使用主题的ASP.NET应用程序。让我们假设我有一个主题名为“MySkin” 我在应用程序的子目录中有一个页面。当我引用一个使用“MySkin”的页面时,我注意到ASP.NET呈现了一个link元素,该元素向上移动到站点的根目录,然后向下移动到App_Themes目录。下面是我在呈现的ASP.NET页面中找到的链接元素示例:ASP.NET-主题和相关引用,asp.net,themes,Asp.net,Themes,我有一个使用主题的ASP.NET应用程序。让我们假设我有一个主题名为“MySkin” 我在应用程序的子目录中有一个页面。当我引用一个使用“MySkin”的页面时,我注意到ASP.NET呈现了一个link元素,该元素向上移动到站点的根目录,然后向下移动到App_Themes目录。下面是我在呈现的ASP.NET页面中找到的链接元素示例: <link href="../../App_Themes/MySkin/theme.css" type="text/css" rel="stylesheet"
<link href="../../App_Themes/MySkin/theme.css" type="text/css" rel="stylesheet" />
是否存在渲染链接元素不使用以下内容的原因:
<link href="/App_Themes/MySkin/theme.css" type="text/css" rel="stylesheet" />
这是浏览器兼容性问题还是有其他原因
我询问的原因是,我正在使用Server.Execute呈现ASP.NET页面,并将结果存储在其他目录中。因此,我更愿意使用第二种方法来引用我的主题的css
谢谢大家! 根据内置的内部类页面ThemeBuildProvider,asp.net为主题目录中包含的css文件创建相对路径
internal void AddCssFile(VirtualPath virtualPath)
{
if (this._cssFileList == null)
{
this._cssFileList = new ArrayList();
}
this._cssFileList.Add(virtualPath.AppRelativeVirtualPathString);
}
要解决您的问题,您可以尝试使用基本标记:
//Add base tag which specifies a base URL for all relative URLs on a page
System.Web.UI.HtmlControls.HtmlGenericControl g = new System.Web.UI.HtmlControls.HtmlGenericControl("base");
//Get app root url
string AppRoot = Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery, "");
g.Attributes.Add("href",AppRoot);
Page.Header.Controls.AddAt(0,g);
使用这种方法的缺点是,如果应用程序url发生更改,链接将断开
为尽量减少此类更改影响,您可以使用html include代替基本标记,以包含包含基本标记的文件,如下所示:
base.html包含:
<base href="http://localhost:50897"></base>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!--#include virtual="base.html" -->
...
</head>
....
</html>
可在应用程序开始请求时创建:
bool writeBase = true;
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (writeBase)
{
writeBase = false;
//Save it to a location that you can easily reference from saved html pages.
string path = HttpContext.Current.Server.MapPath("~/App_Data/base.html");
using (System.IO.TextWriter w = new System.IO.StreamWriter(path, false))
{
w.Write(string.Format("<base href=\"{0}\"></base>", HttpContext.Current.Request.Url.AbsoluteUri.Replace(HttpContext.Current.Request.Url.PathAndQuery, "")));
w.Close();
}
}
}
bool writeBase=true;
受保护的无效应用程序\u BeginRequest(对象发送方,事件参数e)
{
如果(写基)
{
writeBase=false;
//将其保存到一个位置,以便从保存的html页面轻松引用。
string path=HttpContext.Current.Server.MapPath(“~/App_Data/base.html”);
使用(System.IO.TextWriter w=new System.IO.StreamWriter(路径,false))
{
w、 写入(string.Format(“”,HttpContext.Current.Request.Url.AbsoluteUri.Replace(HttpContext.Current.Request.Url.PathAndQuery)”);
w、 Close();
}
}
}
并作为文本控件添加到您的aspx中:
//the path here depends on where you are saving executed pages.
System.Web.UI.LiteralControl l = new LiteralControl("<!--#include virtual=\"base.html\" -->");
Page.Header.Controls.AddAt(0,l);
//此处的路径取决于保存已执行页面的位置。
System.Web.UI.LiteralControl l=新的LiteralControl(“”);
页眉控件地址(0,l);
saved.html包含:
<base href="http://localhost:50897"></base>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!--#include virtual="base.html" -->
...
</head>
....
</html>
...
....
更新:
这是在asp.net development server下测试的,如果作为IIS下的应用程序托管,将无法正确解析Approt。要获取正确的应用程序绝对url,请使用:
/// <summary>
/// Get Applications Absolute Url with a trailing slash appended.
/// </summary>
public static string GetApplicationAbsoluteUrl(HttpRequest Request)
{
return VirtualPathUtility.AppendTrailingSlash(string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Request.ApplicationPath));
}
//
///获取附加尾部斜杠的应用程序绝对Url。
///
公共静态字符串GetApplicationAbsolutionUrl(HttpRequest请求)
{
返回virtualPath实用性.AppendTrailingSlash(string.Format(“{0}://{1}{2}”、Request.Url.Scheme、Request.Url.Authority、Request.ApplicationPath));
}