0

I have an internal ASP.NET MVC site that needs to read an Excel file. The file is on a different server from the one that ASP.NET MVC is running on and in order to prevent access problems I'm trying to copy it to the ASP.NET MVC server.

It works OK on my dev machine but when it is deployed to the server it can't see the path.

This is the chopped down code from the model (C#):

string fPath = HttpContext.Current.Server.MapPath(@"/virtualdir");
string fName = fPath + "test.xlsm"; 

if (System.IO.File.Exists(fName))
{
    // Copy the file and do what's necessary
}
else
{
    if (!Directory.Exists(fPath))
        throw new Exception($"Directory not found: {fPath} ");
    else
        throw new Exception($"File not found: {fName } ");
}

The error I'm getting is

Directory not found:

followed by the path.

The path in the error is correct - I've copied and pasted it into explorer and it resolves OK.

I've tried using the full UNC path, a mapped network drive and a virtual directory (as in the code above). Where required these were given network admin rights (to test only!) but still nothing has worked.

The internal website is using pass through authentication but I've used specific credentials with full admin rights for the virtual directory, and the virtual dir in IIS expands OK to the required folder.

I've also tried giving the application pool (which runs in Integrated mode) full network admin rights.

I'm kind of hoping I've just overlooked something simple and this isn't a 'security feature'.

I found this question copy files between servers asp.net mvc but the answer was to use FTP and I don't want to go down that route if I can avoid it.

Any assistance will be much appreciated.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mick
  • 141
  • 9

1 Answers1

0

First, To be on the safe side that your directory is building correctly, I would use the Path.Combine.

string fName = Path.Combine(fPath, "test.xlsm")

Second, I would check the following post and try some things there as it seems to be a similar issue.

Directory.Exists not working for a network path

If you are still not able to see the directory, there is a good chance the user does not have access to that network path. Likely what happened is the app pool running your application has access to the directory on the server. The production box likely doesn't have that same access. You would have to get with the network engineer to get that resolved.

Alternatively, you could write a Powershell script to run as a user who has access to both the production and the development server to copy the file over to the production server if that is your ultimate goal and your server administrators could schedule it for you if that is allowed in your environment.

Kupokev
  • 169
  • 8