asp.net-coredependency-injectionblazorblazor-server-sidestartup

Is there a way to get the web app domain in Main() (program.cs)?


When my application starts I want to have it create the sitemap.txt dynamically. Doing so requires I have the domain of the website which differs for the dev and production slots.

Is there a way to get this in Main()? I think logically that should be known by the system. But Blazor only has NavigationManager and that service appears to only be available in the context of a page.

If not, I can stuff the value into a singleton service when the first page loads and then have a BackgroundService create the files. Asking here to avoid all that.

Update: The specification for a sitemap.txt is here (along with the spec for sitemap.xml).


Solution

  • The server itself doesn't know about it's domain name, as it's only ever accessed by it's IP. The reason you can see the domain name during a request is because it is sent with the request, or, in the frontend, because the browser knows it.

    You could look at builder.Environment and load the domain name for the current environment from a configuration file.

    You could also make the sitemap.txt point to a controller and generate it every time it's requested, but depending on the size of your website this may take too long. This has the pro that it doesn't need to write to your filesystem.