I'm creating project api in .Net Core 2.1, in my startup.cs, I added:
services.AddSingleton<IPathProvider, PathProvider>();
After that, reate IPathProvider interface and class:
public interface IPathProvider
{
string MapPath(string path);
}
public class PathProvider : IPathProvider
{
private IHostingEnvironment _hostingEnvironment;
public PathProvider(IHostingEnvironment environment)
{
_hostingEnvironment = environment;
}
public string MapPath(string path)
{
var filePath = Path.Combine(_hostingEnvironment.WebRootPath, path);
return filePath;
}
}
And then, in my api cs file, I write code:
private IHostingEnvironment _hostingEnvironment;
public PowerControlController(IHostingEnvironment environment)
{
_hostingEnvironment = environment;
}
public string MapPath(string path)
{
var filePath = Path.Combine(_hostingEnvironment.WebRootPath, path);
return filePath;
}
Now, in main api, I call mappath:
public ActionResult<string> GetListPowerSwitch()
{
try
{
var path = MapPath("../DataDen/DataDen.xml");
return path;
}
catch (Exception ex)
{
return ex.ToString();
}
}
It's work well when debug in local. But, when I publish it to IIS Web Server as new application, it's return ex.ToString() that:
System.ArgumentNullException: Value cannot be null. Parameter name: path1 at System.IO.Path.Combine(String path1, String path2) at LedControlPowerApi.Controllers.PowerControlController.GetListPowerSwitch() in E:\PROJECT EMEC\LedControlPrj\LedControlPowerApi\Controllers\PowerControlController.cs:line 55
Line 55 is: var path = MapPath("../DataDen/DataDen.xml");
Anyone tell me how to fix this bug ? Thanks