在 asp.net 中确定网站的绝对、完全限定 url

发布于 2024-07-05 13:58:33 字数 1266 浏览 4 评论 0原文

无论站点是否位于虚拟目录中,也无论我的代码位于目录结构中的哪个位置,如何才能一致地获取站点的绝对、完全限定的根或基本 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/Det​​ails/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

浅浅淡淡 2024-07-12 13:58:33

在阅读 Rick Strahl 博客中提供的答案时,我发现我真正需要的东西非常简单。 首先,您需要确定相对路径(这对我来说是简单的部分),并将其传递到下面定义的函数中:

VB.NET

Public Shared Function GetFullyQualifiedURL(ByVal s as string) As String
   Dim Result as URI = New URI(HttpContext.Current.Request.Url, s)
   Return Result.ToString
End Function

C#

public static string GetFullyQualifiedURL(string s) {
    Uri Result = new Uri(HttpContext.Current.Request.Url, s);
    return Result.ToString();
}

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

Public Shared Function GetFullyQualifiedURL(ByVal s as string) As String
   Dim Result as URI = New URI(HttpContext.Current.Request.Url, s)
   Return Result.ToString
End Function

C#

public static string GetFullyQualifiedURL(string s) {
    Uri Result = new Uri(HttpContext.Current.Request.Url, s);
    return Result.ToString();
}
音栖息无 2024-07-12 13:58:33

接受的答案假设当前请求已经位于服务器/虚拟根。 尝试这个:

Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath

The accepted answer assumes that the current request is already at the server/virtual root. Try this:

Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath
等待我真够勒 2024-07-12 13:58:33

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

空城缀染半城烟沙 2024-07-12 13:58:33

找到 这里的代码

string appPath = null;

appPath = string.Format("{0}://{1}{2}{3}",
    Request.Url.Scheme,
    Request.Url.Host,
    Request.Url.Port == 80 ? string.Empty : ":" + Request.Url.Port,
    Request.ApplicationPath);

Found this code here:

string appPath = null;

appPath = string.Format("{0}://{1}{2}{3}",
    Request.Url.Scheme,
    Request.Url.Host,
    Request.Url.Port == 80 ? string.Empty : ":" + Request.Url.Port,
    Request.ApplicationPath);
诠释孤独 2024-07-12 13:58:33

您是否尝试过通常在 web.config 文件中配置的 AppSettings.RootUrl ?

Have you tried AppSettings.RootUrl which is usually configured in the web.config file?

じ违心 2024-07-12 13:58:33

您是在谈论用作链接吗?

如果是这样,那么执行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)

梦途 2024-07-12 13:58:33

我目前无法验证这一点,但您是否从另一台计算机尝试过“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文