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.
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.
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.
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.
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.
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(";");
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With