C implements the method of obtaining the absolute address of a website based on the relative address given
- 2021-01-22 05:20:12
- OfStack
This article illustrates the C# implementation of the absolute site address based on the relative address given. Share with you for your reference. The specific analysis is as follows:
. This period of C # code in ASP NET projects can be obtained according to the relative address given absolute access address, for example: given/codes/index php can return https: / / www ofstack. com/codes/index php the absolute address of the results.
/// <summary>
/// Gets the absolute site address based on the relative address given
/// </summary>
/// <param name="localPath"> Relative address </param>
/// <returns> Absolute address </returns>
public static string GetWebPath(string localPath)
{
string path = HttpContext.Current.Request.ApplicationPath;
string thisPath;
string thisLocalPath;
// If it's not the root directory, add it "/" The root directory will add itself "/"
if (path != "/")
{
thisPath = path + "/";
}
else
{
thisPath = path;
}
if (localPath.StartsWith("~/"))
{
thisLocalPath = localPath.Substring(2);
}
else
{
return localPath;
}
return thisPath + thisLocalPath;
}
I hope this article will be helpful to your C# program design.