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);
The bad thing about using this approach is your links will break if the application url gets changed.
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 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();
}
}
}
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 contains:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!--#include virtual="base.html" -->
...
</head>
....
</html>
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));
}