内蔵の内部クラス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));
}