Solving the absolute path of images in ASP. NET

Source: Internet
Author: User

ASP. NET in the page design, if the master template is used, the path of the image will be the most feared, and images will always disappear in different depth references! Some controls also have the same problems. So I finally came up with a method.

For images, we can define a path in the background. The path format is "~ /Image/test.jpg "is then referenced at the front end.

Note: "~" It can only be used on the server.

For example:

Text. aspx. CS

Public String imgpath = "~ /Image/test.jpg ";

Text. aspx

<Asp: Image id = "image1" runat = "server" imageurl = '<% = imgpath %>'/>

 

If it is a div or another background:

We can create an app_themes folder under a directory, and put the CSS file we have written in it. The image is similar to the file.

You can load the CSS in different places in the future.

 

PS: I just want to add one more sentence when talking about the path. For the control, the path in CS is relative to the folder where the control is located, rather than the folder where the page using the control is located.

For example, control 1 has an image1.imageurl = "images/test.jpg" in the background, control 1 is in the "include" folder, and the page using this control is in the root directory, the path of the image is in include/images.test.jpg, but not in images.test.jpg.

 

 

Supplement: (Supplement content from "humble programmer" http://jy147154.bokee.com/view. B? Diaryid = 16134580)

The path of. asax is the same as the path of the file included in ASP. There are two words: Trouble.

. The ASPX page can reference asax in this way.
<% @ Register src = "~ /Usercontrols/xxx. ascx "tagname =" myuc "tagprefix =" uc1 "%>
~ /Indicates the virtual root path relative to the site.

For. asax internal slices, connections, etc:
For Web empty parts such as Hyperlink and image, the path settings are the same as the following:
<Asp: hyperlink navigatorurl = "~ /Yourfolder/yourpage. aspx "runat = server/>

If it is a static hmtl such as <A/> MARK:

Method 1:
Add runat = server to make it an empty HTML Server:

Method 2: absolute path of the Dynamic Process
/images/myimg.gif">

Remember ~ /It only works for the server empty parts of ASP. NET.

 

1. "<% = httpcontext. Current. Request. applicationpath %>/

2. httpcontext. Current. Request. url. getleftpart (uripartial. Authority)

 

3. runat = Server

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.