I am looking for a way to get the root url of my web application so I wouldn't have to use a hardcoded string where I need it.
我正在寻找一种方法来获取我的Web应用程序的根URL,因此我不必在需要它的地方使用硬编码字符串。
So far I've tried to get it this way:
到目前为止,我试图这样做:
var request = HttpContext.Current.Request;
var url = string.Format("{0}://{1}",
request.Url.Scheme,
request.Url.Authority);
but unfortunately this doesn't give me the desired result when executed from the Startup class, I got http://127.0.0.1
但不幸的是,从Startup类执行时,这并没有给我想要的结果,我得到了http://127.0.0.1
3 个解决方案
#1
1
Given a full URL such as "http://example.com/MyApplication/Controller/Action", you can get some of this information from the System.Web.Hosting.HostingEnvironment.ApplicationHost object. You can get the following:
给定完整的URL,例如“http://example.com/MyApplication/Controller/Action”,您可以从System.Web.Hosting.HostingEnvironment.ApplicationHost对象获取一些此类信息。您可以获得以下内容:
ApplicationVirtualPath -> "/MyApplication" SiteName => "Default Web Site" However, you will not be able to get the domain name before an actual request comes in. This is because an IIS website can accept requests for many different domains, some of which are only known via DNS and never configured anywhere.
ApplicationVirtualPath - >“/ MyApplication”SiteName =>“默认网站”但是,在实际请求进入之前,您将无法获取域名。这是因为IIS网站可以接受许多不同域的请求,其中一些只有通过DNS知道,从未在任何地方配置。
For example, your website could respond to both mydomain.com and www.mydomain.com. Which is the correct one that you want to put in your link?
例如,您的网站可以响应mydomain.com和www.mydomain.com。您想要在链接中添加哪个是正确的?
You can configure your IIS website to only accept connections that request a particular host, but that cannot be retrieved from an ASP.NET application.
您可以将IIS网站配置为仅接受请求特定主机但无法从ASP.NET应用程序检索的连接。
Reference : Getting a web application's URI during startup
参考:在启动期间获取Web应用程序的URI
#2
-5
This worked for me:
这对我有用:
var request = HttpContext.Current.Request;
var url = request.Url.Scheme + "://" + request.Url.Authority + request.ApplicationPath.TrimEnd('/');
#3
-5
Try this:
尝试这个:
var url = string.Format("{0}://{1}", request.Url.Scheme, request.Url.Host);
#1
1
Given a full URL such as "http://example.com/MyApplication/Controller/Action", you can get some of this information from the System.Web.Hosting.HostingEnvironment.ApplicationHost object. You can get the following:
给定完整的URL,例如“http://example.com/MyApplication/Controller/Action”,您可以从System.Web.Hosting.HostingEnvironment.ApplicationHost对象获取一些此类信息。您可以获得以下内容:
ApplicationVirtualPath -> "/MyApplication" SiteName => "Default Web Site" However, you will not be able to get the domain name before an actual request comes in. This is because an IIS website can accept requests for many different domains, some of which are only known via DNS and never configured anywhere.
ApplicationVirtualPath - >“/ MyApplication”SiteName =>“默认网站”但是,在实际请求进入之前,您将无法获取域名。这是因为IIS网站可以接受许多不同域的请求,其中一些只有通过DNS知道,从未在任何地方配置。
For example, your website could respond to both mydomain.com and www.mydomain.com. Which is the correct one that you want to put in your link?
例如,您的网站可以响应mydomain.com和www.mydomain.com。您想要在链接中添加哪个是正确的?
You can configure your IIS website to only accept connections that request a particular host, but that cannot be retrieved from an ASP.NET application.
您可以将IIS网站配置为仅接受请求特定主机但无法从ASP.NET应用程序检索的连接。
Reference : Getting a web application's URI during startup
参考:在启动期间获取Web应用程序的URI
#2
-5
This worked for me:
这对我有用:
var request = HttpContext.Current.Request;
var url = request.Url.Scheme + "://" + request.Url.Authority + request.ApplicationPath.TrimEnd('/');
#3
-5
Try this:
尝试这个:
var url = string.Format("{0}://{1}", request.Url.Scheme, request.Url.Host);