ASP. NET converts an absolute path under a Web site to a virtual path

Source: Internet
Author: User
I often use a feature, but I have not found any related solutions on the Internet. Today I have written two methods for converting absolute paths to virtual paths through project application.
Converts an absolute path under a Web site to a virtual path relative to a specified page.
/// <Summary>
/// Convert the absolute path under the web site to the virtual path relative to the specified page
/// </Summary>
/// <Param name = "page"> current page pointer, which is generally this </param>
/// <Param name = "specifiedpath"> absolute path </param>
/// <Returns> virtual path, type such as: http://www.cnblogs.com/</returns>
Public static string convertspecifiedpathtore Lativepathforpage (page, string specifiedpath)
{
// Root directory Virtual Path
String virtualpath = page. Request. applicationpath;
// Absolute path of the root directory
String pathrooted = hostingenvironment. mappath (virtualpath );
// Page Virtual Path
String pagevirtualpath = page. Request. path;

If (! Path. ispathrooted (specifiedpath) specifiedpath. indexof (pathrooted) =-1)
{< br> throw new exception (string. format ("\" {0} \ "is a virtual path rather than an absolute path! ", Specifiedpath);
}

   // Convert to relative path
   // (The test showed that the server that comes with pathrooted in vs2005 does not seem to run in the root directory or virtual directory of IIS,
   // "\" Will be added at the end of this place, and some will not. Please check for the sake of insurance)
   If (pathrooted. substring (pathrooted. Length-1, 1) = "\\")
   {
       Specifiedpath = specifiedpath. Replace (pathrooted ,"/");
   }
   Else
   {
       Specifiedpath = specifiedpath. Replace (pathrooted ,"");
   }

   String relativepath = specifiedpath. Replace ("\\","/");

   String [] pagenodes = pagevirtualpath. Split ('/');

   // Subtract the last page and the previous "" Value
Int pagenodescount = pagenodes. Length-2;

 

   For (INT I = 0; I <pagenodescount; I ++)
   {
       Relativepath = "/..." + relativepath;
   }

   If (pagenodescount> 0)
   {
       // If "..." exists, remove the first "/"
       Relativepath = relativepath. substring (1, relativepath. Length-1 );
   }

   Return relativepath;
}

The second method is extracted from the first part of the first method.
Converts an absolute path under a Web site to a virtual path.
/// <Summary>
/// Convert the absolute path under the web site to a virtual path
/// Note: Non-web sites do not convert
/// </Summary>
/// <Param name = "page"> current page pointer, which is generally this </param>
/// <Param name = "specifiedpath"> absolute path </param>
/// <Returns> virtual path, for example :~ /</Returns>
Public static string convertspecifiedpathtoreLativepath (page, string specifiedpath)

{
   String virtualpath = page. Request. applicationpath;

   String pathrooted = hostingenvironment. mappath (virtualpath );

   If (! Path. ispathrooted (specifiedpath) specifiedpath. indexof (pathrooted) =-1)
   {
       Return specifiedpath;
   }

   If (pathrooted. substring (pathrooted. Length-1, 1) = "\\")
   {
       Specifiedpath = specifiedpath. Replace (pathrooted ,"~ /");
   }
   Else
   {
       Specifiedpath = specifiedpath. Replace (pathrooted ,"~ ");
}

 

   String relativepath = specifiedpath. Replace ("\\","/");
   Return relativepath;
}

There is nothing to say about converting a virtual path to an absolute path. The httprequest. mappath method is dedicated to doing this.

From: http://blog.sina.com.cn/s/blog_4efce4d10100clcj.html ~ Type = v5_one & label = rela_prevarticle

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.