简体   繁体   English

ASP网站根网址

[英]ASP site root url

Sorry for question that is already asked few times and on other forums, but I really didn't find a solution that gives what I need. 抱歉,已经在其他论坛上已多次问过这个问题,但我确实没有找到能够满足我需求的解决方案。

I have an ASP.NET MVC site. 我有一个ASP.NET MVC网站。 And in one of the controllers I need to find the root url of the site (For example http://myhost:7777/MyVirtualDir/ ). 在其中一个控制器中,我需要找到该站点的根URL(例如http:// myhost:7777 / MyVirtualDir / )。 I need it as is, not any relative urls - only full absolute ROOT url of the site. 我需要它,没有任何相对的URL,只有站点的完整绝对ROOT URL。 The reason is that I will use this url from other application to access service, hosted on site. 原因是我将使用其他应用程序中的该URL来访问站点上托管的服务。 I tried many dirrerent variants and proposed solutions from other forums, including: 我尝试了许多直接变体并从其他论坛中提出了解决方案,包括:

VirtualPathUtility.ToAbsolute("~") // Returns "/" - it returns relative url
Url.Content("~")  //Returns "/MyVirtualDir/" - only virtual directory path part

and all other ToAbsolute return relative paths. 和所有其他ToAbsolute返回相对路径。

I invented the following : 我发明了以下内容

Request.Url.ToString().Substring(0, Request.Url.ToString().Length - Request.Url.PathAndQuery.Length + Url.Content("~").Length);

Looks like it works . 看起来很有效 But there is too much magic here. 但是这里有太多魔术。 Is there a normal way to retrieve such path ? 有正常的方法来检索这种路径吗?

As far as I can tell, there is no built-in functionality to grab specifically the root of the application with virtual paths in a single call. 据我所知,没有内置功能可以在单个调用中使用虚拟路径专门抓住应用程序的根。 The simplest solution I can find is this: 我能找到的最简单的解决方案是:

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

You may also find this useful: http://www.west-wind.com/weblog/posts/2009/Dec/21/Making-Sense-of-ASPNET-Paths 您可能还会发现这很有用: http : //www.west-wind.com/weblog/posts/2009/Dec/21/Making-Sense-of-ASPNET-Paths

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM