skip to Main Content

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


  1. 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:

    public class WebServerInfoService : IWebServerInfoService
    {
        private readonly List<string> boundUrls = new List<string>();
        private readonly object lockObject = new object();
    
        public void AddBoundUrl(string url)
        {
            lock (lockObject)
            {
                boundUrls.Add(url);
            }
        }
    
        public List<string> GetBoundUrls()
        {
            lock (lockObject)
            {
                return boundUrls.ToList(); // Return a copy of the list to prevent modification by consumers
            }
        }
    }
    

    Main:

     app.UseUrls(urls =>
        {
            foreach (var url in urls)
            {
                webServerInfoService.AddBoundUrl(url);
            }
        });
    

    Razor View:

    
    @inject IWebServerInfoService webServerInfoService
    
    <!DOCTYPE html>
    <html>
    <head>
        <title>Bound URLs</title>
    </head>
    <body>
        <h1>Bound URLs</h1>
        <ul>
            @foreach (var url in webServerInfoService.GetBoundUrls())
            {
                <li>@url</li>
            }
        </ul>
    </body>
    </html>
    
    Login or Signup to reply.
  2. Create a custom configuration class to hold the URLs.

    public class WebServerUrls
    {
        public List<string> Urls { get; set; } = new List<string>();
    }
    

    In your Program.cs file, add the registration for this custom configuration class as a singleton service:

    builder.Services.AddSingleton<WebServerUrls>();
    // ...
    
    
    var builder = WebApplication.CreateBuilder(args);
    // ...
    
    var app = builder.Build();
    // ...
    
    await app.StartAsync();
    var webServerUrls = app.Services.GetRequiredService<WebServerUrls>();
    webServerUrls.Urls.AddRange(app.Urls);
    
    // ...
    

    In your Razor Pages view, inject the WebServerUrls class using the @inject directive and access the bound URLs:

    @page
    @model YourPageModel
    
    @inject WebServerUrls WebServerUrls
    
    <h1>Bound URLs</h1>
    
    <ul>
        @foreach (var url in WebServerUrls.Urls)
        {
            <li>@url</li>
        }
    </ul>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search