In ASP.NET, I have used the Server.MapPath() method to get the physical path. Now in core, I can't do that, I have used IHostingEnvironment, but it can only get wwwroot path.
Asked
Active
Viewed 5,004 times
3
-
I look some article ,this [link](https://stackoverflow.com/questions/40562149/can-not-access-virtual-directory-under-virtual-dotnet-core-api-application-on-ii) make me know how to solve the quesion. – willim Aug 22 '17 at 01:52
-
https://stackoverflow.com/a/55380851/7149454 – Nick Kovalsky Mar 27 '19 at 15:26
2 Answers
0
Use HostingEnvironment.MapPath like bellow
HostingEnvironment.MapPath("~/page.aspx")
or this to get root folder path
HostingEnvironment.MapPath("/")
Fatehi_Alqadasi
- 360
- 4
- 12
-
1I have already try it,but it not have this "MapPath" method. I need something else to do ? – willim Aug 21 '17 at 07:21
-
-
1HostingEnvironment's namespace is "Microsoft.AspNetCore.Hosting.Internal" ,and vs tell me it not contain "MapPath" definitions – willim Aug 21 '17 at 07:44
-
it is in System.Web namespace use it like this System.Web.HostingEnvironment.MapPath("/") – Fatehi_Alqadasi Aug 21 '17 at 07:47
-
1
0
You should use FileProvider
var contents = _fileProvider.GetDirectoryContents("");
return contents.Select(i => i.PhysicalPath);
You can check out my demo code
Alex Nguyen
- 1,020
- 12
- 26
-
I have already try,it's a get file list from a directory ,Not to get physic path – willim Aug 21 '17 at 08:41
-
you can get the physical path by its property = PhysicalPath contents.Select(i => i.PhysicalPath); => it returns the physical path – Alex Nguyen Aug 21 '17 at 08:43
-
I publish a website in IIS,and set a directory to virtual directory.Now I want to get the directory physic path to save file. – willim Aug 21 '17 at 08:45
-
-
inject IHostingEnvironment in your controller and then you can get your root path, var rootPath =_env.ContentRootPath; – Alex Nguyen Aug 21 '17 at 09:06
-
-
i' sorry,it seem is not a good method.Now I write the physical path to appsetting to resolve this problem .Look forward to better method . How ever ,I'm still thank you very much! – willim Aug 21 '17 at 09:35