I’d like to display my web server’s bound URLs from within a Razor Pages view. I can access them from within Main
like so:
var builder = WebApplication.CreateBuilder(args);
//...
var app = builder.Build();
//...
await app.StartAsync();
//...
// Debug here...
… by having a breakpoint at "Debug here" and examining app.Urls
(app is a Microsoft.AspNetCore.Builder.WebApplication
), but I can’t add this during the DI configuration because it only gets populated after StartAsync
. How, then, can I get access to this information from within a Razor Pages view running from within that web application?
2
Answers
To display the web server’s bound URLs within a Razor Pages view, create a service to store and access this information then inject that service data into Razor view.
service:
Main:
Razor View:
Create a custom configuration class to hold the URLs.
In your
Program.cs
file, add the registration for this custom configuration class as a singleton service:In your Razor Pages view, inject the WebServerUrls class using the @inject directive and access the bound URLs: