在asp.net中确定站点的绝对完全限定URL

时间:2022-10-10 20:24:38

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.

无论站点是否在虚拟目录中,无论我的代码在目录结构中的哪个位置,我如何始终获得站点的绝对,完全限定的根或基本URL?我已经尝试了我能想到的每一个变量和功能,并没有找到一个好方法。

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/

我希望能够获取当前站点的URL,即http://www.example.com,或者如果它是虚拟目录,请访问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:

这是我尝试过的一些事情和结果。唯一包含我想要的整块(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 Request.Url.Host: localhost
  • Request.Url.LocalPath:/DNN441/Default.aspx Request.Url.Host:localhost

  • Request.Url.PathAndQuery: /DNN441/Default.aspx?TabId=56&ctl=Details&mid=374&ItemID=1

7 个解决方案

#1


12  

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:

通过阅读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();
}

#2


7  

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

接受的答案假定当前请求已经在服务器/虚拟根目录。尝试这个:

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

#3


5  

There is some excellent discussion and ideas on Rick Strahl's blog

Rick Strahl的博客上有一些很好的讨论和想法

EDIT: I should add that the ideas work with or without a valid HttpContext.

编辑:我应该补充说,这些想法可以使用或不使用有效的HttpContext。

EDIT2: Here's the specific comment / code on that post that answers the question

EDIT2:这是该帖子上回答问题的具体评论/代码

#4


2  

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);

#5


0  

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

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

#6


0  

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)

现在,对于客户端来说,将“〜/”传递给Control :: ResolveUrl方法将为您提供所需的内容。 (http://msdn.microsoft.com/en-us/library/system.web.ui.control.resolveurl.aspx)

#7


0  

I have no way to validate this at the moment but have you tried "Request.Url.AbsoluteUri" from another machine?

我目前无法验证这一点,但您是否尝试过另一台机器上的“Request.Url.AbsoluteUri”?

It occurs to me that as far as your machine is concerned it's browser is requesting from localhost.

我发现,就你的机器而言,它的浏览器是从localhost请求的。

I could be wrong though but I think request is relative to the browser and not the webserver.

我可能错了,但我认为请求是相对于浏览器而不是网络服务器。

#1


12  

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:

通过阅读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();
}

#2


7  

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

接受的答案假定当前请求已经在服务器/虚拟根目录。尝试这个:

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

#3


5  

There is some excellent discussion and ideas on Rick Strahl's blog

Rick Strahl的博客上有一些很好的讨论和想法

EDIT: I should add that the ideas work with or without a valid HttpContext.

编辑:我应该补充说,这些想法可以使用或不使用有效的HttpContext。

EDIT2: Here's the specific comment / code on that post that answers the question

EDIT2:这是该帖子上回答问题的具体评论/代码

#4


2  

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);

#5


0  

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

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

#6


0  

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)

现在,对于客户端来说,将“〜/”传递给Control :: ResolveUrl方法将为您提供所需的内容。 (http://msdn.microsoft.com/en-us/library/system.web.ui.control.resolveurl.aspx)

#7


0  

I have no way to validate this at the moment but have you tried "Request.Url.AbsoluteUri" from another machine?

我目前无法验证这一点,但您是否尝试过另一台机器上的“Request.Url.AbsoluteUri”?

It occurs to me that as far as your machine is concerned it's browser is requesting from localhost.

我发现,就你的机器而言,它的浏览器是从localhost请求的。

I could be wrong though but I think request is relative to the browser and not the webserver.

我可能错了,但我认为请求是相对于浏览器而不是网络服务器。