在 asp.net 中确定网站的绝对、完全限定 url
无论站点是否位于虚拟目录中,也无论我的代码位于目录结构中的哪个位置,如何才能一致地获取站点的绝对、完全限定的根或基本 url? 我已经尝试了我能想到的所有变量和函数,但没有找到好的方法。
我希望能够获取当前站点的网址,即 http://www.example.com或者如果它是虚拟目录,http://www.example.com/DNN/
这里有一些我尝试过的事情和结果。 唯一包含我想要的整个部分的 (http://localhost:4471/DNN441) 是 Request .URI.AbsoluteURI:
- Request.PhysicalPath: C:\WebSites\DNN441\Default.aspx
- Request.ApplicationPath: /DNN441
- Request.PhysicalApplicationPath: C:\WebSites\DNN441\
- MapPath: C:\WebSites\DNN441\DesktopModules\Articles\Templates\Default.aspx
- RawURL: /DNN441/
- ModuleTesting/Articles/tabid/56/ctl/Details/mid/374/ItemID/1/Default.aspx Request.Url.AbsoluteUri: http://localhost:4471/DNN441/Default.aspx
- Request.Url.AbsolutePath: /DNN441/Default.aspx
- Request.Url.LocalPath: /DNN441/Default.aspx 请求.Url.Host:本地主机
- Request.Url.PathAndQuery: /DNN441/Default.aspx?TabId=56&ctl=详细信息&mid=374&ItemID=1
How can I consistently get the absolute, fully-qualified root or base url of the site regardless of whether the site is in a virtual directory and regardless of where my code is in the directory structure? I've tried every variable and function I can think of and haven't found a good way.
I want to be able to get the url of the current site, i.e. http://www.example.com or if it's a virtual directory, http://www.example.com/DNN/
Here's some of the things I've tried and the result. The only one that includes the whole piece that I want (http://localhost:4471/DNN441) is Request.URI.AbsoluteURI:
- Request.PhysicalPath: C:\WebSites\DNN441\Default.aspx
- Request.ApplicationPath: /DNN441
- Request.PhysicalApplicationPath: C:\WebSites\DNN441\
- MapPath:
C:\WebSites\DNN441\DesktopModules\Articles\Templates\Default.aspx - RawURL:
/DNN441/ModuleTesting/Articles/tabid/56/ctl/Details/mid/374/ItemID/1/Default.aspx - Request.Url.AbsoluteUri: http://localhost:4471/DNN441/Default.aspx
- Request.Url.AbsolutePath: /DNN441/Default.aspx
- Request.Url.LocalPath: /DNN441/Default.aspx Request.Url.Host: localhost
- Request.Url.PathAndQuery:
/DNN441/Default.aspx?TabId=56&ctl=Details&mid=374&ItemID=1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在阅读 Rick Strahl 博客中提供的答案时,我发现我真正需要的东西非常简单。 首先,您需要确定相对路径(这对我来说是简单的部分),并将其传递到下面定义的函数中:
VB.NET
C#
In reading through the answer provided in Rick Strahl's Blog I found what I really needed was quite simple. First you need to determine the relative path (which for me was the easy part), and pass that into the function defined below:
VB.NET
C#
接受的答案假设当前请求已经位于服务器/虚拟根。 尝试这个:
The accepted answer assumes that the current request is already at the server/virtual root. Try this:
Rick Strahl 的博客上有一些精彩的讨论和想法
编辑:我应该补充一点,无论有没有有效的 HttpContext,这些想法都可以工作。
编辑2:这是该帖子的具体评论/代码回答问题
There is some excellent discussion and ideas on Rick Strahl's blog
EDIT: I should add that the ideas work with or without a valid HttpContext.
EDIT2: Here's the specific comment / code on that post that answers the question
找到 这里的代码:
Found this code here:
您是否尝试过通常在 web.config 文件中配置的 AppSettings.RootUrl ?
Have you tried AppSettings.RootUrl which is usually configured in the web.config file?
您是在谈论用作链接吗?
如果是这样,那么执行
goes to root
将带您进入网络根目录的默认文件。现在,对于客户端,将“~/”传递给 Control::ResolveUrl 方法将为您提供您正在寻找的内容。 (http://msdn.microsoft.com /en-us/library/system.web.ui.control.resolveurl.aspx)
Are you talking about for use as links?
if so, then doing this
<a href='/'>goes to root</a>
will take you to the default file of the web root.Now, for client side, doing, passing "~/" to the Control::ResolveUrl method will provide you what you're looking for. (http://msdn.microsoft.com/en-us/library/system.web.ui.control.resolveurl.aspx)
我目前无法验证这一点,但您是否从另一台计算机尝试过“Request.Url.AbsoluteUri”?
我发现就您的机器而言,它的浏览器正在从本地主机请求。
我可能是错的,但我认为请求与浏览器有关,而不是与网络服务器有关。
I have no way to validate this at the moment but have you tried "Request.Url.AbsoluteUri" from another machine?
It occurs to me that as far as your machine is concerned it's browser is requesting from localhost.
I could be wrong though but I think request is relative to the browser and not the webserver.