.NET中有个mappath方法是转成物理路径的,但是我却找不到现成的方法转成虚拟路径,自己写了一个,以做备用!
///
<summary>
把~/niunantest.aspx转化成http://localhost:3212/ModuleTest/niunantest.aspx
///
/// </summary>
/// <param name="path"> 如:~/niunantest.aspx </param>
/// <returns></returns>
public static string GetVirsualURL( string path)
{
path = path.Replace( " ~ " , "" );
HttpRequest request = HttpContext.Current.Request;
string port = request.Url.Port == 80 ? "" : " : " + request.Url.Port.ToString();
if (request.ApplicationPath == " / " )
{
return @" http:// " + request.Url.Host + port + path;
}
else
{
return @" http:// " + request.Url.Host + port + request.ApplicationPath + path;
}
}
///
/// </summary>
/// <param name="path"> 如:~/niunantest.aspx </param>
/// <returns></returns>
public static string GetVirsualURL( string path)
{
path = path.Replace( " ~ " , "" );
HttpRequest request = HttpContext.Current.Request;
string port = request.Url.Port == 80 ? "" : " : " + request.Url.Port.ToString();
if (request.ApplicationPath == " / " )
{
return @" http:// " + request.Url.Host + port + path;
}
else
{
return @" http:// " + request.Url.Host + port + request.ApplicationPath + path;
}
}