2020-12-09 05:08

Change templates to use environment variable for urls.

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()


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • weixin_39658966 weixin_39658966 5月前

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


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

    点赞 评论 复制链接分享
  • weixin_39752215 weixin_39752215 5月前

    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.

    点赞 评论 复制链接分享
  • weixin_39662263 weixin_39662263 5月前

    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.

    点赞 评论 复制链接分享
  • weixin_39658966 weixin_39658966 5月前

    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.

    点赞 评论 复制链接分享
  • weixin_39752215 weixin_39752215 5月前

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

    点赞 评论 复制链接分享
  • weixin_39662834 weixin_39662834 5月前

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

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

    点赞 评论 复制链接分享