During developmentProgramThe directory structure is clear. We hope that many different file directories will be created.
For example, the structure is as follows:
| Root
| PIC
| Web
| Usercontrol
In the web directory, how do we get the image path in the PIC directory?
Method 1:
Let's first look at the solution of ASP. NET startkit timetracker:
The global class of ASP. NET startkit timetracker defines a public method:
Public Static String Getapplicationpath (httprequest request)
{
String Path = String . Empty;
Try
{
If(Request. applicationpath! = "/")
Path=Request. applicationpath;
}
Catch (Exception E)
{
ThrowE;
}
Return Path;
}
Make a call as needed, for example:
<A href = '<% = Global. getapplicationpath (request) %>/<% # (ASPnet. starterkit. timetracker. businesslogiclayer. tabitem)
Container. dataitem). PATH %> '>
<% # (ASPnet. starterkit. timetracker. businesslogiclayer. tabitem) container. dataitem). name %>
</A>
I modified this method:
I will first define a page base class.
Public class pagebase: system. Web. UI. Page
Let other aspx pages in the system inherit pagebase.
Attributes under the base class definition
Protected String Apppath
{
Get
{
String Path = String. empty;
Try
{
If ( " / " ! = Request. applicationpath)
{
Path=Request. applicationpath;
}
}
Catch (Exception E)
{
ThrowE;
}
Return Path;
}
}
On the my ASPX page, bind the following attributes to get the image.
'>
Method 2:
It is also a common method.
<Asp: Image id = "image1" runat = "server" imageurl = "../PIC/register.gif"> </ASP: Image>
In fact, the server control supports another path Representation Method :"~ ", Equivalent to httprequest. applicationpath
<Asp: Image runat = "server" id = "image1" imageurl = "~ /PIC/register.gif "> </ASP: Image>
Non-server controls can also be like this:
Method 3:
The path is saved in Web. config during deployment on your machine. Then, the image path is determined in the background. CS using configuration. paiettings.
This method is the worst.
Summary: I personally think that method 1 is the best and most flexible. It is also the most used method I have seen in many Microsoft examples.
I don't know if there are other methods. Please kindly advise.