Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do You Access the `applicationUrl` Property Found in launchSettings.json from Asp.NET Core 3.1 Startup class?

I am currently creating an Asp.NET Core 3.1 API Application. In it, I have a launchSettings.json that looks like the following:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:61392",
      "sslPort": 44308
    }
  },
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "weatherforecast",
      "environmentVariables": {
        "Key": "Value",
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "Application.API": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "weatherforecast",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://localhost:5001;http://localhost:5000"
    }
  }
}

In particular, I am interested in obtaining the applicationUrl values defined in this file. Unfortunately, I do not see any obvious way to access them outside of loading the file directly via JSON serialization and probing the values there.

As such, I am looking for a more elegant solution. Is there a way to access these values from within the Startup.Configure and/or Startup.ConfigureServices methods?

I am thinking of something like:

public void Configure(IApplicationBuilder builder)
{
    var url = builder.ApplicationServices.GetRequiredService<IServerHostingEnvironment>().ApplicationUrl
}

For reference, I did see this question, but it seems that this requires an HTTP request. In my case, I am still configuring the server before a request has been made.

For further reference and context: I am building a .NET GitHub App application, and am making the corresponding equivalent of the Smee.io client, whose Javascript code can be found here.

The client takes a source (via Server-sent events) and sends it to a target. I got the source figured out and configured, but am ironically having trouble accessing and establishing the target URL (the applicationUrl seen above) in an obvious and configured fashion.

Finally, I realize I could hardcode the value in appSettings.json, but that would then mean I would be maintaining the same value in two places -- one in appSettings.json and one in the already-existing launchSettings.json. I would rather keep it to one place to reduce maintenance burden, if possible.

like image 398
Mike-E Avatar asked Dec 18 '19 18:12

Mike-E


People also ask

What is the use of launchSettings json in ASP.NET Core?

The launchSettings. json file is used to store the configuration information, which describes how to start the ASP.NET Core application, using Visual Studio. The file is used only during the development of the application using Visual Studio. It contains only those settings that required to run the application.

What is Properties launchSettings json?

launchSettings. json, which is placed in the Properties folder of a project, describes how the application can be launched — the command to execute, whether the browser should be opened, which environment variables should be set, and so on.

How do I edit json launchSettings?

You can directly edit the launchSettings. json file in Visual Studio for Mac, or you can use project options to edit it. To get to the project options, right-click your project and select Options. Select Run > Configurations > Default.

Where is launchSettings json located?

In the Properties folder in an ASP.NET Core project, you can find the launchSettings. json file, which contains settings that control how your web app is started on your development machine.


2 Answers

I was actually looking for something similar myself recently.

It turns out you can determine what URL's were passed to the application by Visual Studio during a debugging session (from applicationUrl within launchSettings.json) By looking at the environmental variables.

Although bearing in mind this is probably only useful for debugging within VStudio IDE which uses the launchSettings.json file

var urls = Environment.GetEnvironmentVariable("ASPNETCORE_URLS").Split(";");
like image 159
garlicbread Avatar answered Oct 07 '22 00:10

garlicbread


Have not tested it but I use this pattern to access appsettings.json.

In startup.cs add launchsettings.json to the configuration builder:

public Startup(IConfiguration configuration)
    {
        var builder = new ConfigurationBuilder().AddJsonFile(Path.Combine(Directory.GetCurrentDirectory(), "Properties", "launchSettings.json"));
        Configuration = builder.Build();
    }

public IConfiguration Configuration { get; }

Add it as a service:

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton <IConfiguration> (Configuration);
}

Inject it in controller and get key value:

public class SomeController: ControllerBase {  
    private IConfiguration _iconfiguration;  
    public SomeController(IConfiguration iconfiguration) {  
        _iconfiguration = iconfiguration;  
    }
void SomeMethod(){
string appUrl = _iConfiguration.GetValue<string>("applicationUrl","someDefaultValue")

} }
Hope this helps you out!

like image 3
Zonorai Avatar answered Oct 07 '22 00:10

Zonorai