CSS, scripts, images, and so on will inevitably be referenced in the template page. If the paths of these files are simple to use relative paths, the directories that reference the template will change and errors will occur; if the absolute path is used, it is not flexible enough. If the application directory changes, it may cause a large number of modifications. Asp.net supports a path that starts with a wavy line, such "~ /", Use it to solve the problem, for example:
<Link rel = "stylesheet" media = "screen" type = "text/css" href = "<% = ResolveClientUrl ("~ /Css/global.css ") %>"/>
Of course, if you think that every path should be written as dynamic, and there is a page base class, you can use another method:
All the paths are directly written as relative to the application directory, such:
<Link rel = "stylesheet" media = "screen" type = "text/css" href = "~ /Css/global.css "/>
Of course, HTML does not support this path by default. In this case, we need to use PageBase. The Code is as follows (as if it was extracted from DNN code ):
- Public abstract class PageBase: Page
- {
- Protected override void Render (HtmlTextWriter writer)
- {
- StringWriter stringWriter = new StringWriter ();
- HtmlTextWriter htmlWriter = new HtmlTextWriter (stringWriter );
- Base. Render (htmlWriter );
- String html = stringWriter. ToString ();
- # Region conversion relative path
- MatchCollection collection = Regex. matches (html, "<(a | link | img | script | input | form ). [^>] * (href | src | action) = (// "| '| )(. [^ // "'] *) (//" |' |) [^>] *> ", RegexOptions. ignoreCase );
- Foreach (Match match in collection)
- {
- If (match. Groups [match. Groups. Count-2]. Value. IndexOf ("~ ")! =-1)
- {
- String url = this. Page. ResolveUrl (match. Groups [match. Groups. Count-2]. Value );
- Html = html. Replace (match. Groups [match. Groups. Count-2]. Value, url );
- }
- }
- # Endregion
- Writer. Write (html );
- }
- }