Search code examples
c#dotnet-aspire

In .NET Aspire, how to get the resolved endpoint to create a Link


Using the new .NET Aspire Let's explore a scenario involving two web apps: the main app, and the login app:

var login = builder.AddProject<Projects.LoginApp_Web>("login");

builder.AddProject<Projects.AspireApp_Web>("main-app")
    .WithReference(login); // not sure if will be need it

Questions:

  1. Main App Endpoint to Login App: In the main-app, specifically on a Razor page, how can we create an endpoint that directs users to the login app?
  2. Furthermore, Two-Way Navigation, Is it possible to establish a two-way link between the main app and the login app? For instance, can the main app provide a link to the login app, and vice versa, How would we achieve this?

It seems the ServiceDiscovery is the responsible for that, but I am not able to figure it out how to use it.


Solution

  • Thank you @AkalankaDissanayake,

    1. Do not need .WithReference(login); instead need to get http or https url and save it in app Environment.
    mainApp.WithEnvironment("LoginUrl", login.GetEndpoint("http"));
    
    // Similarly you can do the other way around as well.
    login.WithEnvironment("mainAppUrl", mainApp.GetEndpoint("http"));
    
    1. Now this value can be access by the configuration
    var configuration = builder.Configuration;
    configuration.GetRequiredValue("LoginUrl");
    
    // which get the value from here:
    configuration[name]
    

    Original code: