Skip to content

Working directory for ASP.NET Core WebApplication is set to project folder #5053

Closed

Description

@Compufreak345

Visual Studio Version:
16.1.6

Summary:
When I run a newly created empty WebApplication and start Debugging the WorkingDirectory is the projects root folder. When I create an empty console application it is the Output Directory. I expect both to be the same (and am quite sure this was the case some time in the past?).

Steps to Reproduce:

  1. Create an empty web application project with .Net Core 2.1 or 2.2.

  2. Paste this code:

    Console.WriteLine("CurrentDirectory in Main: {0}", System.IO.Directory.GetCurrentDirectory());
  3. Run the project without IIS (IIS will show the same behavior but logging it is a bit more complicated).

Expected Behavior:
The output is CurrentDirectory in Main: {ProjectPath}\bin\Debug\netcoreapp2.2

Actual Behavior:
The output is CurrentDirectory in Main: {ProjectPath}

User Impact:
I can't use a library that expects the WorkingDirectory to be in the output directory without adding some code changing the working directory to where it should have been in the first place.

Other notes
This issue seems to have been reappearing and fixed for console applications and now seems to appear for web applications as well.

See #2239 and #589

Activity

  1. pi3k14 commented on Jul 16, 2019

    @pi3k14

    I think Microsoft have to make a clear statement, is working directory for console and web apps supposed to be the same, or are they promoting what @davkean wrote in issue #2239.
    For console apps it has to be output directory.

  2. pi3k14 commented on Jul 16, 2019

    @pi3k14

    This websdk #238 might be relevant.

  3. bricelam commented on Sep 17, 2019

    @bricelam

    Issue #3619 is about changing console apps to match the behavior of web apps

  4. AcellaEDU commented on Dec 25, 2019

    @AcellaEDU

    It is actual for netcore 3.1 (AST.NET CORE 3.1 MVC/SPA )

  5. added
    Triage-Approved
    Triage-Investigate
    and removed
    Triage-Approved
    on Jan 20, 2020
  6. davkean commented on Jan 20, 2020

    @davkean
    Member

    Triage: This needs investigation as to what the right behavior is, please reach out to @BillHiebert on ASP.NET tooling to figure out if there's work for either team to do here.

  7. added this to the 16.7 milestone on Jan 20, 2020
  8. assigned and unassigned on Jan 27, 2020
  9. added
    Feature-Debugging
    on Jan 28, 2020
  10. davkean commented on Jan 28, 2020

    @davkean
    Member

    Please factor in #3619 when you look into this.

  11. 7 remaining items

  12. removed their assignment
    on Nov 2, 2021
  13. jjmew commented on Nov 16, 2021

    @jjmew
    Contributor

    We have reached out to the Web Tools team for additional input

  14. self-assigned this
    on Dec 3, 2021
  15. added
    Triage-Investigate
    on Dec 3, 2021
  16. added this to the 17.1 milestone on Dec 3, 2021
  17. tmeschter commented on Dec 3, 2021

    @tmeschter
    Contributor

    Likely By Design.

  18. drewnoakes commented on Dec 7, 2021

    @drewnoakes
    Member

    ASP.NET applications use the project folder as the process working directory on purpose. This enables a workflow where you can modify resources (CSS, JS, HTML, images, ...) in VS and have those picked up in a browser via hot reload or manual refresh without having to build or publish.

    Changing this would break well established workflows.

  19. jmevel commented on Dec 7, 2021

    @jmevel

    ASP.NET applications use the project folder as the process working directory on purpose. This enables a workflow where you can modify resources (CSS, JS, HTML, images, ...) in VS and have those picked up in a browser via hot reload or manual refresh without having to build or publish.

    Changing this would break well established workflows.

    Well, this is by design for the front then.
    So I'll continue changing the default directory in all my APIs then...

  20. peterhirn commented on Jan 9, 2022

    @peterhirn

    @jmevel Can you elaborate on how you accomplish this?

    I tried setting the working directory in the project file

    <PropertyGroup>
      <RunWorkingDirectory>$(TargetDir)</RunWorkingDirectory>
    <PropertyGroup>

    But TargetDir, TargetPath, etc. are returning empty values.

  21. jmevel commented on Jan 9, 2022

    @jmevel

    @jmevel Can you elaborate on how you accomplish this?

    I tried setting the working directory in the project file

    <PropertyGroup>
      <RunWorkingDirectory>$(TargetDir)</RunWorkingDirectory>
    <PropertyGroup>
    

    But TargetDir, TargetPath, etc. are returning empty values.

    Sorry maybe my comment wasn't clear enough. I was talking about changing the current directory simply with Directory.SetCurrentDirectory

  22. dmytroshchotkin commented on Feb 7, 2023

    @dmytroshchotkin

    As nicely described (here), you can use
    Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)

  23. panagiotidisc commented on Jan 9, 2024

    @panagiotidisc

    ASP.NET applications use the project folder as the process working directory on purpose. This enables a workflow where you can modify resources (CSS, JS, HTML, images, ...) in VS and have those picked up in a browser via hot reload or manual refresh without having to build or publish.

    Changing this would break well established workflows.

    The real workaround for this """feature""" is to set the "workingDirectory" in launchSettings.json to $(TargetDir). Or from the menu Debug -> Debug Properties -> Launch Profiles -> Working Directory = $(TargetDir)

    But I have to note that I find the fact that ASP.NET projects were hardcoded to behave peculiarly in many ways (including this), instead of having a proper template for ASP.NET with an appropriate launchSettings.json frankly, quite ludicrous

  24. jmevel commented on Feb 5, 2024

    @jmevel

    ASP.NET applications use the project folder as the process working directory on purpose. This enables a workflow where you can modify resources (CSS, JS, HTML, images, ...) in VS and have those picked up in a browser via hot reload or manual refresh without having to build or publish.
    Changing this would break well established workflows.

    The real workaround for this """feature""" is to set the "workingDirectory" in launchSettings.json to $(TargetDir). Or from the menu Debug -> Debug Properties -> Launch Profiles -> Working Directory = $(TargetDir)

    But I have to note that I find the fact that ASP.NET projects were hardcoded to behave peculiarly in many ways (including this), instead of having a proper template for ASP.NET with an appropriate launchSettings.json frankly, quite ludicrous

    Awesome, I didn't know we can set this from the launchSettings.json, I should have checked before.

    Thanks for the tips

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Feature-DebuggingTriage-Investigate

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions