Master page path (masterpage path)

Source: Internet
Author: User
Tags to domain
Masterpage path
------------------
From: http://hi.baidu.com/%B7%E7%D3%EA%B9%CA%C8%CB/blog/item/897ac13582b06c1090ef39e3.html
About the image path in masterpage,
If the relative path relative is referenced.
If the absolute path is used ~ /Xxx.gif: when it is attached to a virtual directory, the problem also occurs.
One solution is to create a CSS file under the app_cheme directory and use
. Hidebar
{
Background: URL (http://www.cnblogs.com/pic/custom_images/delval_top_new.jpg );
}
Then, reference class = "hidebar" where the image needs to be referenced in the template tag ", in this way, when the template calculates the path, it starts to calculate from the CSS path, and the CSS path is always fixed, so it can avoid confusion during inheritance, in the virtual directory, the path is calculated from the bottom up, so no problem occurs.
------------------
From: http://www.cnblogs.com/lijunming/articles/522747.html
Path processing in ASP. NET 2.0 masterpage
Use vs. net2005 to Develop ASP. NET 2.0 applications Program The masterpage concept is proposed, which is equivalent to a template.
Set the file path in a masterpage. When a file not in the same directory uses this masterpage, the link to this path may fail. In this case, it is not appropriate to use the relative path.
You can use the following method for processing:
String _ url = page. Request. url. tostring ();
String _ ret = _ URL. substring (0, _ URL. tolower (). indexof ("/your virtual directory name/", 0) + "/your virtual directory name /";
The obtained _ RET is the absolute path of the web project. You can add links to any file path in the project.
The tolower () method is used to avoid the case sensitivity problem of the URL. The name of the virtual directory we specify may have uppercase and lowercase letters, and the user input is another thing, most likely all are in lower case.
------------------
From: http://www.ninedns.com/asp.net/2007102520419.html
Solution to the image path in ASP. NET masterpage
Generally, there are three methods to express the link path: absolute path, relative path, and root directory-based path. In addition, for ASP. NET Server controls, you can also use "~" To replace the root directory.
In masterpage and user controls, images are often used as backgrounds or beautiful buttons. However, when you specify the SRC or background of an image, errors often occur due to link paths.
1. Use absolute path: an absolute file path like "D:" XXX "xxx.gif" is generally not desirable. You can consider using the URL method to write it as http: // xxxx/XX/xxx.gif ". But the disadvantage is not conducive to transplantation, for example, the current site address is http://www.xxx.net, if one day the site is more the http://www.xxx.com, then all the link addresses are invalid, need to change, difficult to maintain.
2. use relative path: Use the path relative to the page location, such as ".. "Images" xxx.gif ", so that both masterpage and user control can be correctly displayed, but if the page inheriting masterpage is placed in a different folder, or the page using the user control is not in the same folder, the correct image location will not be found on this page!
3. based on the root directory path: such as: <a href = "/XXX/xxx.gif"> in ASP. during net2.0 debugging, the virtual directory is not created and cannot be correctly displayed (I am not very sure, I did not confirm ^_^ ). "~" Cannot be used on HTML Tag elements of non-server controls. To specify the path.
So what should we do to make the image visible during design? I used CSS to do this. For the elements and controls to display the image, we can write a simple CSS to locate the image, because the position of the CSS file is generally not changed (in app_theme/themename/xxx.css ), this method also works.
. Hidebar
{
Height: 56px;
Width: 5px;
Cursor: hand;
Background-image: URL (http://images.cnblogs.com/xxx.gif );
} Then, we only need to fill in hidebar in the cssclass of the corresponding elements and controls, so that every page inheriting masterpage or using user controls can correctly display images.
I think there should be other better methods, and I hope someone can give me some advice.
-- Robin Lu's blog
------------------
From: http://superkam.blog.163.com/blog/static/228256482008316560726/
Masterpage of. NET 2.0 is quite convenient, but if a page in different paths references masterpage, the relative path set in masterpage will be incorrect. For example, the masterpage path is domain/testmasterpage. master, and masterpage references a relative address .. /admin/default. aspx, if domain/admin/test. if aspx inherits from this masterpage, the inherited test. the address on the ASPX page is changed to domain/admin/default. aspx, this is not what we want. Of course, you can use <% = resolveclienturl ("~ /Admin/default. aspx ") %>.
To solve this problem, you can write a page base class to identify the address in the base class and automatically convert it. The following are basic classes 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 );
}
}
After the base class is completed, let the previous domain/admin/test. ASPX page inherit this base class (masterpage does not need to inherit it), as long ~ The addresses starting with/are automatically converted to absolute addresses (~ /Admin/default. aspx will be automatically converted to domain/admin/default. aspx ). In this way, ~ is used in both materpage ~ The address starting with/. Any other pages that inherit from it can obtain the correct absolute address.
------------------
From: http://forums.asp.net/t/1054133.aspx
Specifying Javascript file path on master pages
<Link runat = "server" href = "~ /Scripts/myfile. js "/>
<SCRIPT type = "text/JavaScript" src = '<% = resolveurl ("~ /Swfobject. js ") %> '> </SCRIPT>
------------------
From: http://mosalem.blogspot.com/2006/07/master-page-and-content-page-path-in.html
If (request. currentexecutionfilepath. tolower (). Contains ("/admin /"))
{
// Not in the same folder
Boddy. Style [htmltextwriterstyle. backgroundimage] = "../images/bg.gif ";
}
Else
Boddy. Style [htmltextwriterstyle. backgroundimage] = "images/bg.gif ";
------------------
From: http://www.codeverge.net/ng.asp-net-forum.master_pages_themes_and_navigation_controls/masterpage-path-for-scripts
1. for CSS
<Link runat = "server" href = "~ /CSS/css.css "rel =" stylesheet "type =" text/CSS "/>
2. For JavaScript, check following thread
Http://forums.asp.net/thread/1390505.aspx
------------------
From: http://www.csharper.net/blog/using_the_tilde __~ _ In_asp_net_everywhere ___ not_just_controls _. aspx
Using the Tilde (~ ) In ASP. NET everywhere-not just controls!
Protected override void render (htmltextwriter writer)
{
Memorystream MS = new memorystream ();
Streamwriter Sw = new streamwriter (MS );
Htmltextwriter HTW = new htmltextwriter (SW );

Base. Render (HTW );
HTW. Flush ();
Ms. Position = 0;

Textreader TR = new streamreader (MS );
String output = tr. readtoend ();
String newoutput = replacewithapppath (output );
Writer. Write (newoutput );

HTW. Close ();
Sw. Close ();
Ms. Close ();
}

Public static string replacewithapppath (string Str)
{
String apppath = httpcontext. Current. Request. applicationpath;

// Ensure the app path ends w/a slash
If (! Apppath. endswith ("/"))
Apppath + = "/";

Return Str. Replace ("~ /", Apppath );
}
------------------

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.