0

Net Core web app that has a utility where users can upload a photo. It's using a API Controller to do the actual uploading. I created a folder in the Solution called ImageStorage where the folder path looks like this:

G:\GameDevelopment\DungeonMaxer\MainApp\wwwroot\ImageStorage

and I also created a folder under ImageStorage called uploads.

I am trying to test it out locally(IIS Express) and I added this line to the controller:

[HttpPost("upload")]
public async Task<IActionResult> PostAsync(IFormFile file)
{

    // parent folder for storage
    var serverStoragePath = Server.MapPath(@"~/ImageStorage");

    // uploads folder inside the parent
    var fileRoute = Path.Combine(serverStoragePath, "uploads");

    // get file extension

    string extension = System.IO.Path.GetExtension(theFile.FileName);

    // create a new name for storage
    string name = Guid.NewGuid().ToString().Substring(0, 8) + extension;

    // get the link to the file
    string link = Path.Combine(fileRoute, name);

    using (FileStream writerFileStream = System.IO.File.Create(link))
    {
        await stream.CopyToAsync(writerFileStream);
        writerFileStream.Dispose();
    }

    // Return the file path as json
    Hashtable imageUrl = new Hashtable();
    imageUrl.Add("link", "/uploads/" + name);

    return Json(imageUrl);
}

But when I run the project locally and put a break point on the line, var serverStoragePath = Server.MapPath(@"~/ImageStorage");, I get this error:

 System.NullReferenceException: 'Object reference not set to an instance of an object.'

I also tried wwwroot\ImageStorage but that gave me the same error.

Is there a way to access this path ?

thanks!

SkyeBoniwell
  • 6,345
  • 12
  • 81
  • 185
  • 1
    Could it be just a permission problem? I assume that you have already checked with the debugger if the final filename is correct. – Steve Mar 23 '21 at 19:16
  • @Steve well I know the path exists in my file system. I also looked up Server.MapPath on the Microsoft site and I think I'm using it right. – SkyeBoniwell Mar 23 '21 at 19:19
  • 1
    what is the value of serverStoragePath after the call to the Server.MapPath?... Also that might not be where the applicaiton base path is set to – Jonathan Alfaro Mar 23 '21 at 19:23
  • @JonathanAlfaro I just put a breakpoint there on that line of code and noticed I am recieving a `System.NullReferenceException: 'Object reference not set to an instance of an object.'` error – SkyeBoniwell Mar 23 '21 at 19:26
  • 4
    Wait. Are you trying to use Server.MapPath in an Asp.Net Core project? No way, you need to use _IWebHostEnvironment_ through DI and then use the property _WebRootPath_ – Steve Mar 23 '21 at 19:31
  • @Steve yes, I need to get a folder I created for uploaded images. – SkyeBoniwell Mar 23 '21 at 20:12
  • 1
    Just Path.Combine WebRootPath with your folder/s – Steve Mar 23 '21 at 20:33

1 Answers1

2

IHostingEnvironment is used in .Net Core 2.0 IWebHostEnvironment has replacing IHostingEnvironment in .Net Core 3.0.

public class HomeController : Controller
{
    private IHostingEnvironment Environment;
 
    public HomeController(IHostingEnvironment _environment)
    {
        Environment = _environment;
    }
 
    public IActionResult Index()
    {
        string wwwPath = this.Environment.WebRootPath;
        string contentPath = this.Environment.ContentRootPath;
 
        return View();
    }
}
Juanma Feliu
  • 1,298
  • 4
  • 16