开发者

Get name of virtual directory?

开发者 https://www.devze.com 2022-12-19 03:40 出处:网络
I\'m us开发者_JAVA百科ing Request.ApplicationPath to learn the name of the Virtual Directory in which I\'m running.Is there a more reliable way?Request.ApplicationPath is perfectly reliable way of get

I'm us开发者_JAVA百科ing Request.ApplicationPath to learn the name of the Virtual Directory in which I'm running. Is there a more reliable way?


Request.ApplicationPath is perfectly reliable way of getting the virtual directory and works always when you have the HttpContext and can ask for the Request data.

For further processing and extracting parts of the path, take a look at the VirtualPathUtility class.


You need to use Request.ApplicationPath. That is what it is designed for.

Editing to go with your comment.

Since you want a 'cleaner' way to handle the slash, I recommend creating a utility function that returns the application path with the logic in it to deal with the slash as you see fit.


Use this function in C#:

public static String GetHost()
{
    var request = HttpContext.Current.Request;
    return request.Url.Scheme + "://" + request.ServerVariables["HTTP_HOST"] + request.ApplicationPath;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消