Three methods for setting paths in ASP. NET

Source: Internet
Author: User

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

 

Related Article

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.