2009-06-03 33 views
2

テーマを使用するASP.NETアプリケーションがあります。私は "MySkin"というテーマを持つふりをしましょう。ASP.NET - テーマと相対参照

私のアプリケーションのサブディレクトリにあるページがあります。私が "MySkin"を使用するページを参照すると、ASP.NETはサイトのルートまで歩いてリンクしてから、App_Themesディレクトリに移動するリンク要素をレンダリングすることに気付きました。ここで私はレンダリングされたASP.NETページで見つかった例のlink要素である:

<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を参照するために第2の方法を使用することを好むでしょう。

ありがとうございました!

答えて

0

内蔵の内部クラスPageThemeBuildProviderによると、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が変更されるとリンクが壊れることです。

base.htmlが含まれています:

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

これは可能性が

は、このような変更の影響を最小限に抑えるには、次のようにベースタグを含むファイルが含まれるようにベースタグの代わりに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(); 
       } 
      }    
     } 

とあなたの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が含まれています

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

UPDATE: APPROOTが正しく解決されませんIISでアプリケーションとしてホスト場合、これは、asp.net開発サーバーで試験しました。正しいアプリケーションを取得するには、絶対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)); 
} 
関連する問題