Server.MapPath does not return the correct physical path associated with the virtual directory in a subfolder of Web-App

I create a website and map it to the subdirectory D:\MyWebApp and Plugins . I create a virtual directory and map it to D:\Project\Presentation\Web\Accounting . the problem is when I want to get the physical path http://localhost/Plugins/Accounting , I get the wrong result.

both Server.MapPath("~/Plugins/Accounting") and Server.MapPath("/Plugins/Accounting") return 'D: \ MyWebApp \ Plugins \ Accounting' that do not physically exist. I want to get D:\Project\Presentation\Web\Accounting . I search for it and just get below lines:

  • Server.MapPath (".") Returns the current physical directory of the executable file (for example, aspx)
  • Server.MapPath ("..") returns the parent directory
  • Server.MapPath ("~") returns the physical path to the application root
  • Server.MapPath ("/") returns the physical path to the root of the domain name (not necessarily the same as the application root)

but in my case (virtual directory in a subfolder) it does not work !!! I use IIS8 and Asp.Net-Mvc 4 and C # 4. how can I get this?

+4
source share
3 answers

try it

remove "/ plugin"

by request http://localhost/Plugins/Accounting The Server.MapPath ("~ /") method provides the path to your application folder.

 Server.MapPath("~/Accounting/");//path to your Accounting folder 
+1
source

Use it

 Request.PhysicalApplicationPath + "Plugins\\Accounting\\ 
0
source

The problem is that you have a virtual directory that will reset, which asp.net considers the base directory of your application. Do I need to be in a virtual directory? If so, you can try checking Request.ApplicationPath to find it.

0
source

Source: https://habr.com/ru/post/1489613/


All Articles