Search code examples
asp.net-mvc

Why does Server.MapPath("~") give different results in different controllers?


I have one controller, WorkingController.cs with a method UploadDocument() which contains the code

var dir = Server.MapPath("~/Working");
Directory.CreateDirectory(dir);

In a different area I have another Controller, NotWorkingController.cs with a similar method UploadDocument() which contains the code

var dir = Server.MathPath("~/NotWorking");
Directory.CreateDirectory(dir);

When the code is run on my live and test site, the "Working" folder is created at C:\inetpub\wwwroot\Working. However the NotWorking folder is created at C:\inetpub\wwwroot\My_Application_Name\NotWorking

How does MVC determine where the root folder is? And how do I make this consistent?

When I publish my project any documents uploaded by users that are inside C:\inetpub\wwwroot\My_Application_Name get deleted!

I've looked in the various AreaRegistraion.cs and web.config files and the RouteConfig.cs file but I can't find anything that configures where the application root is.


Solution

  • Have you checked your IIS settings?

    Go to IIS, right click the website -> Basic Settings and check the physical path of your site.

    I suspect that you might have an application under the website, check the physical path for the application as well... this should help you find out to what physical path your application is Mapped.

    enter image description here