例如,我有:
string AbsImgURL = "~/MyStuff/Images/MsgBoxIcon/MyImg.jpg";
我希望它是:
string AbsImgURL = "../../MyStuff/Images/MsgBoxIcon/MyImg.jpg"
(因为我目前在页面"〜/ UI / Pages / Default.aspx",这是从根目录两层深处)
答案 0 :(得分:0)
谢谢你,我终于通过使用VirtualPathUtility.MakeRelative得到了它。代码如下:
/// <summary>Convert and URL Absoluate Path to Relative Path
///(For Example: "~/MyStuff/Images/MsgBoxIcon/MyImg.jpg" ==> "../../MyStuff/Images/MsgBoxIcon/MyImg.jpg"
///(Assuming client is currently at the page "~/UI/Pages/Default.aspx", which is two level deep from the root).</summary>
private static string ConvertAbsoluteToRelativePath(string Input)
{
//Init
string Output = "";
//Get Current URL (Ex: http://MyWebSite//...)
string CurrentURL = HttpContext.Current.Request.Url.AbsolutePath;
//Convert to Relative Path
Output = VirtualPathUtility.MakeRelative(CurrentURL, Input);
//Finally
return Output;
}