ASP.NET - 主题和相对引用 [英] ASP.NET - Themes and Relative Referencing

查看:97
本文介绍了ASP.NET - 主题和相对引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用主题ASP.NET应用程序。让我们pretend我有一个名为MySkin的主题。

I have an ASP.NET application that uses themes. Let's pretend I have a theme named "MySkin".

我有一个网页,在我的应用程序的子目录。当我参考,使用My​​Skin一个页面,我注意到,ASP.NET将呈现走过来的站点的根目录,然后下入App_Themes文件目录的链接元素。下面是一个例子链接元素我在渲染ASP.NET页面发现:

I have a page that is in a sub-directory of my application. When I reference a page that uses "MySkin", I noticed that ASP.NET renders a link element that walks up to the root of the site and then down into the App_Themes directory. Here is an example link element I found in a rendered ASP.NET page:

<link href="../../App_Themes/MySkin/theme.css" type="text/css" rel="stylesheet" />

是否有一个原因,呈现链接元素不改用以下内容:

Is there a reason that the rendered link element does not use the following instead:

<link href="/App_Themes/MySkin/theme.css" type="text/css" rel="stylesheet" />

这是一个浏览器兼容性问题,还是有其他原因?

Is this a browser compatibility issue or is there another reason?

我问的原因是因为我呈现我的ASP.NET页面使用使用Server.Execute并将结果存储在不同的目录中。正因为如此,我想preFER使用第二种方式来引用我的主题的CSS。

The reason I am asking is because I am rendering my ASP.NET page using Server.Execute and storing the result in a different directory. Because of this, I would prefer to use the second way to reference my theme's css.

感谢您!

推荐答案

根据内置的内部类PageThemeBuildProvider,asp.net创建相对路径中包含的主题目录css文件

According to the built-in internal class PageThemeBuildProvider, asp.net create relative path for css files included in the theme directory

internal void AddCssFile(VirtualPath virtualPath)
{
    if (this._cssFileList == null)
    {
        this._cssFileList = new ArrayList();
    }
    this._cssFileList.Add(virtualPath.AppRelativeVirtualPathString);
}

要解决你的问题,你可以尝试使用基本标记:

To overcome your problem you may try using base tag:

//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得到改变你的链接将中断。

The bad thing about using this approach is your links will break if the application url gets changed.

要尽量减少这种变化的影响,你可以使用HTML包括代替基本标记的,包括含有你的基地标签的文件如下:

To minimize such change impact, you may use html include in place of the base tag to include a file containing your base tag as follows:

base.html文件包括:

base.html contains:

<base href="http://localhost:50897"></base>

这可以在应用程序创建开始请求:

this could be created on application begin request:

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

和添加为文字控制你的aspx:

and added as a literal control to your 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);

saved.html包括:

saved.html contains:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!--#include virtual="base.html" -->
...
</head>
 .... 
</html>

更新:
这是在asp.net开发服务器进行测试,如果托管的IIS下的应用为approot将无法正确解析。为了得到正确的应用绝对URL使用:

UPDATE: This was tested under asp.net development server, if hosted as application under IIS the AppRoot will not be resolved correctly. To get the correct applications absolute url use:

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

这篇关于ASP.NET - 主题和相对引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆