The following lists three common methods for setting paths in ASP. NET development.
Default. aspx. CSCodeAs follows:
Public partial class_ Default: System. Web. UI.Page
{
Public StringGeturl (String_ IMG)
{
// Obtain the ASP. NET application on the serverProgramThe root path of the virtual application.
StringRoot = request. applicationpath;
// Return the absolute path under the image root directory and add double quotation marks.
Return'"'+ Root +"/Img /"+ _ IMG +'"';
}
Protected voidPage_load (ObjectSender,EventargsE)
{}
}
The default. aspx code is as follows:
<Body>
<FormID= "Form1"Runat= "Server">
<IMGSRC= "/Specify three addresses/img/book1.jpg"ALT= "HTML Tag"/>
<ASP:ImageRunat= "Server"Imageurl= "~ /Img/book2.jpg"Alternatetext= "Alt properties of server controls"/>
<IMGSRC=<%= Geturl ("book3.jpg ")%> ALT= "Get path from. CS code"/>
<! -- <% = Geturl ("book3.jpg"); %> This method is incorrect! Do not add a semicolon (;) to the end of the called function. -->
</Form>
</Body>
Note: "/three addresses are specified for this site"Root directory folder(The root directory starts from the root folder ).
After running, the "View Source File" code is as follows:
<Body>
<FormName= "Form1"Method= "Post"Action= "Default. aspx"ID= "Form2">
<Div>
<InputType= "Hidden"Name= "_ Viewstate"ID= "_ Viewstate"
Value= "/Wepdwulltewmtm4mzg2mdjkzbus1fryus31/0rzbi7gtrqlsein"/>
</Div>
<IMGSRC= "/Specify three addresses/img/book1.jpg"ALT= "HTML Tag"/>
<IMGSRC= "Img/book2.jpg"ALT= "Alt attribute of server control"Style= "Border-width: 0px;"/>
<IMGSRC= "/Specify three addresses/img/book3.jpg"ALT= "Get path from. CS code"/>
</Form>
</Body>
Summary:
○ The HTML Tag Method is the simplest;
○ The <asp:/> server control method is the safest;
○ Adding <% = %> to the background code is the most flexible (Note: Do not add a semicolon (;) after the called function );
How to obtainSite Virtual PathCorrespondingDisk Physical path?
StringPath = server. mappath ("IMG");
Response. Write (PATH );
// Print the result: C: \ web_test \ The specified method of the three addresses \ img
Additional: various methods for request to obtain URL Information
In ASP. NET programming, you often need to use request to obtain URL information. There are multiple methods in the request to obtain URL Information. The results obtained by various methods are listed below.
The URL of the test is a http://www.test.com/testweb/default.aspx and the result is as follows:
Request.Applicationpath:/Testweb
Request.Currentexecutionfilepath:/Testweb/default. aspx
Request.Filepath:/Testweb/default. aspx
Request.Path:/Testweb/default. aspx
Request.Pathinfo:
Request.Physicalapplicationpath: E: \ www \ testweb \
Request.Physicalpath: E: \ www \ testweb \ default. aspx
Request.Rawurl:/Testweb/default. aspx
Request.URL. absolutepath:/Testweb/default. aspx
Request.URL. absoluteuri: Http://www.test.com/testweb/default.aspx
Request.URL. Host: Www.test.com
Request.URL. localpath:/Testweb/default. aspx
In ASP. NET programming, you often need to use request to obtain URL information. There are multiple methods in the request to obtain URL Information. The results obtained by various methods are listed below.
The URL of the test is a http://www.test.com/testweb/default.aspx and the result is as follows:
Request.Applicationpath:/Testweb
Request.Currentexecutionfilepath:/Testweb/default. aspx
Request.Filepath:/Testweb/default. aspx
Request.Path:/Testweb/default. aspx
Request.Pathinfo:
Request.Physicalapplicationpath: E: \ www \ testweb \
Request.Physicalpath: E: \ www \ testweb \ default. aspx
Request.Rawurl:/Testweb/default. aspx
Request.URL. absolutepath:/Testweb/default. aspx
Request.URL. absoluteuri: Http://www.test.com/testweb/default.aspx
Request.URL. Host: Www.test.com
Request.URL. localpath:/Testweb/default. aspx