我如何获得一个ASP.NET应用程序的完整虚拟路径 [英] How do I get the complete virtual path of an ASP.NET application
问题描述
我如何知道我的应用程序目前正在举办的完整的虚拟路径?例如:
How do I know the the complete virtual path that my application is currently hosted? For example:
http://www.mysite.com/myApp
或
http://www.mysite.com/myApp/mySubApp
我知道的应用程序路径的Htt prequest但它只返回我的应用程序正在托管文件夹的名称,但我如何获得初始的一部分吗?
I know the application path of HttpRequest but it only returns the folder name that my application is currently hosted, but how do I get the initial part?
推荐答案
路径的域名部分是不是真正的应用程序本身的属性,而是取决于请求的URL。你也许能够从许多不同的主机名到达一个网站。要获得在当前请求相关联,与当前应用程序的虚拟路径沿着域名,你可以这样做:
The domain name part of the path is not really a property of the application itself, but depends on the requesting URL. You might be able to reach a single Web site from many different host names. To get the domain name associated with the current request, along with the virtual path of the current application, you could do:
Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath
从技术上讲,一个应用程序是在IIS中定义的虚拟目录, Request.ApplicationPath
返回这一点。如果你想获得的文件夹中当前的要求的处理,你可以这样做:
Technically, an "application" is a virtual directory defined in IIS and Request.ApplicationPath
returns exactly that. If you want to get the folder in which the current request is handled, you can do this:
VirtualPathUtility.GetDirectory(Request.Path)
ASP.NET不知道如何,如果它没有被定义为在IIS虚拟目录的子应用程序从一个更大的应用程序区分开来。如果没有IIS注册,它只是看到了整个事情作为一个单一的应用程序。
ASP.NET has no idea how to distinguish your sub-application from a bigger application if it's not defined as a virtual directory in IIS. Without registering in IIS, it just sees the whole thing as a single app.
这篇关于我如何获得一个ASP.NET应用程序的完整虚拟路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!