0

I have downloaded one ASP.Net Core 2.2 MVC project. It is running fine. But when I created new Asp.Net core 2.2 MVC project in my system and copied all code from downloaded project one GetCurrentDirectory function is not working properly.

I am using below code :

Path.Combine(Directory.GetCurrentDirectory(), location, fileName)

My project is in D drive. The project which I downloaded is showing correct path. But the project I created is showing below path :

C:\Program Files\IIS Express\data\people.json

Can any one help me in explaining why is this happening?

Shardul
  • 186
  • 1
  • 12
  • 1
    The `GetCurrentDirectory` method returns the **current directory from the OS' point of view**, not the directory the executable was started from. – dymanoid Jan 11 '19 at 13:14
  • That code could easily result in security breaches. If `location` came from user input, a value like `..\..\..\Windows` would map to `C:\Windows`. Check [What is the equivalent of Server.MapPath in ASP.NET Core?](https://stackoverflow.com/questions/49398965/what-is-the-equivalent-of-server-mappath-in-asp-net-core) – Panagiotis Kanavos Jan 11 '19 at 13:23
  • *Why* are you creating an absolute path in the first place? Why not load this in the configuration phase from the path specified in Content Root? – Panagiotis Kanavos Jan 11 '19 at 13:28

1 Answers1

1

When using IIS in-process hosting in ASP.NET Core 2.2.0, the current directory is set to the directory of the IIS application itself, so in your case when debugging it is the location of IIS Express: C:\Program Files\IIS Express

This behaviour will be fixed in ASP.NET Core 2.2.2 to be more intuitive and match the location of the application on disk: https://github.com/aspnet/AspNetCore/pull/6150

Martin Costello
  • 7,092
  • 4
  • 48
  • 62
  • But I have downloaded a project and if I am running that project it is showing me correct location of my project. – Shardul Jan 11 '19 at 13:33
  • The debugging/hosting settings might be different between those two things. I can't really tell without the project itself to look at. – Martin Costello Jan 11 '19 at 14:05
  • 1
    Have a look in the `.csproj` files - your new one will contain `InProcess` and the downloaded one will not. If you need a workaround (other than turning off InProcess), I answered something similar [here](https://stackoverflow.com/questions/53846333/serilog-does-not-write-log-to-file-while-using-inprocess-hosting-model-in-asp-ne/53859202#53859202). – Kirk Larkin Jan 11 '19 at 14:56
  • `InProcess` is also the default, so would be used if no value was set at all. Alternatively, the value might be being overridden in `Web.config` on the `` element. – Martin Costello Jan 11 '19 at 15:55
  • That's not true - [`OutOfProcess` is the default](https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/aspnet-core-module?view=aspnetcore-2.2#hosting-models). – Kirk Larkin Jan 11 '19 at 15:59
  • Ah, I must have got confused with what was originally planned in earlier previews. – Martin Costello Jan 11 '19 at 16:27
  • @kirk you are correct. AspNetCoreHostingModel is missing in project. Would you like to suggest any fix while Inprocess mode can be kept as is? – Shardul Jan 11 '19 at 16:36
  • Until 2.2.2 is released (as Martin has pointed out here), you'll need to use the workaround shown in my answer I linked above. – Kirk Larkin Jan 11 '19 at 16:49