How can I get the root domain URI in ASP.NET?

后端 未结 14 1437
庸人自扰
庸人自扰 2020-12-12 16:15

Let\'s say I\'m hosting a website at http://www.foobar.com.

Is there a way I can programmatically ascertain \"http://www.foobar.com/\" in my code behind (i.e. witho

14条回答
  •  春和景丽
    2020-12-12 16:31

    To get the entire request URL string:

    HttpContext.Current.Request.Url
    

    To get the www.foo.com portion of the request:

    HttpContext.Current.Request.Url.Host
    

    Note that you are, to some degree, at the mercy of factors outside your ASP.NET application. If IIS is configured to accept multiple or any host header for your application, then any of those domains which resolved to your application via DNS may show up as the Request Url, depending on which one the user entered.

提交回复
热议问题