3

I'm trying to determine the current domain name from within the Startup.Cs module of an ASP.NET Core 2.0 Razor pages application. The website will be bound to several different domains and I want to load the appropriate _layout/theme based on this.

I have been searching, but I can't figure out how to determine which domain was used to reach the site.

Any help would be greatly appreciated.

Thanks.

Robert Hill
  • 323
  • 1
  • 8
  • 16
  • 1
    Just need to look at the host header, passed in the request. – John Wu May 30 '18 at 00:09
  • Okay, I've seen several notes about viewing the host header from the request, however, I'm not sure how to access the request in the Startup.cs module. – Robert Hill May 30 '18 at 15:10
  • The host header isn't available during startup. You can't determine the domain at startup anyway, due to "the web sitesite will be bound to several domains." The domain is associated with a request, not globally with the site. See [How to get host header](https://stackoverflow.com/questions/1638669/how-to-get-host-header-from-httpcontext-asp-net) and [Is it possible to get host header during startup](https://stackoverflow.com/questions/29241346/is-it-possible-to-get-the-host-in-application-start) – John Wu May 30 '18 at 15:37

1 Answers1

6

You have access to the request object if your controller inherits from Controller

public IActionResult About()
    {
        ViewData["Message"] = $"{this.Request.Scheme}://{this.Request.Host}{this.Request.PathBase}";

        return View();
    }
Truc
  • 386
  • 4
  • 12