File Path reference on the master page in ASP. NET 2.0

Source: Internet
Author: User
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 ):

 

  1. Public abstract class PageBase: Page
  2. {
  3. Protected override void Render (HtmlTextWriter writer)
  4. {
  5. StringWriter stringWriter = new StringWriter ();
  6. HtmlTextWriter htmlWriter = new HtmlTextWriter (stringWriter );
  7. Base. Render (htmlWriter );
  8. String html = stringWriter. ToString ();
  9. # Region conversion relative path
  10. MatchCollection collection = Regex. matches (html, "<(a | link | img | script | input | form ). [^>] * (href | src | action) = (// "| '| )(. [^ // "'] *) (//" |' |) [^>] *> ", RegexOptions. ignoreCase );
  11. Foreach (Match match in collection)
  12. {
  13. If (match. Groups [match. Groups. Count-2]. Value. IndexOf ("~ ")! =-1)
  14. {
  15. String url = this. Page. ResolveUrl (match. Groups [match. Groups. Count-2]. Value );
  16. Html = html. Replace (match. Groups [match. Groups. Count-2]. Value, url );
  17. }
  18. }
  19. # Endregion
  20. Writer. Write (html );
  21. }
  22. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.