File paths referenced on the master page in ASP. NET 2.0 (Collection)

Source: Internet
Author: User

I have never studied it. I can't take it anymore. Please find a solution:

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 applicationProgramDirectory changes 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,CodeAs shown in the following figure (as shown in dnn code ):

 

Public abstract class pagebase: Page
{
Protected override void render (htmltextwriter writer)
{
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 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 );

}

}

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.