aspnet / Templates

This repo is OBSOLETE - please see the README file for information
Other
150 stars 57 forks source link

Change templates to use environment variable for urls. #574

Closed glennc closed 8 years ago

glennc commented 8 years ago

This will allow the Docker tools team to not modify user code after a new app is created.

var urls = Environment.GetEnvironmentVariable("ASPNETCORE_URLS") ?? "http://localhost:5000";
var host = new WebHostBuilder()
            .UseUrls(urls)
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();
mikeharder commented 8 years ago

I think templates should use the following, instead of hardcoding http://localhost:5000:

UseUrls(Environment.GetEnvironmentVariable("ASPNETCORE_URLS"))

If the environment variable is not set, then UseUrls(null) will be called, and Hosting will use the default of http://localhost:5000.

sayedihashimi commented 8 years ago

What would be the experience if you have a team and some use VS and others editors like VS Code or Notepad++? For the VS users, we can take care of setting the env var. For the other users, if I understand this correctly, it would require them to set an env var on the command prompt for each project that needs to be launched. The local url of the app is important because users will refer to a particular url for local development in other projects.

phenning commented 8 years ago

@sayedihashimi I think for non-docker scenarios, we wouldn't set the environment variable and then hosting would just use localhost:5000 like Mike mentioned, thus keeping the same behavior we have today.

Docker would set the environment variable for their runtime scenario.

mikeharder commented 8 years ago

Correct, there should be no difference from current behavior if the environment variable is not set. UseUrls(null) is a no-op. This just makes it easier for Docker Tooling (or any other customer scenarios) to change the address via environment variable.

We didn't want to add a fixed environment variable for this in the framework itself (in part because it's not discoverable), but it seems reasonable in a template, and serves as documentation for how to change the URL.

sayedihashimi commented 8 years ago

I see so this is independent of IIS Express, I missed that part.

glennc commented 8 years ago

This isn't needed now that WebHostBuilder will read the variable itself instead of having to have it passed in.

@mikeharder I was fairly sure UseUrls throws if is passed null. We could change that of course.