简体   繁体   English

MVC:File.exists返回false

[英]MVC : File.exists return false

I have a MVC method like this : 我有这样的MVC方法:

public ActionResult DownloadDocument(string filePath){
   //Other stuff
   if (!System.IO.File.Exists(filePath))
   {
       FileLogger.Debug($"DownloadDocument : {filePath} NotFound");
   }
   //Other stuff
}

filePath looks like => \\\\ServerB\\Documents\\General\\Administration\\Doc1.pdf filePath看起来像=> \\\\ServerB\\Documents\\General\\Administration\\Doc1.pdf

In local I have a IISExpress (I know that's not great but no choice...) and a data server (Server B). 在本地,我有一个IISExpress(我知道那不是很好,但别无选择...)和一个数据服务器(服务器B)。 File is on server B. It works really good, I can retrieve my file. 文件在服务器B上。它的工作原理非常好,我可以检索文件。

When publishing, I have a IIS on Server A and a the same data Server (Server B). 发布时,我在服务器A和同一台数据服务器(服务器B)上都有一个IIS。 FilePath is still on server B. In this case, I can't retrieve my file because this line returns False : FilePath仍在服务器B上。在这种情况下,我无法检索文件,因为此行返回False:

System.IO.File.Exists(filePath)

Searching a solution, I see ServerMapPath but it's not helping because filePath is on Server B, not Server A. 在搜索解决方案时,我看到ServerMapPath,但是它没有帮助,因为filePath在服务器B而不是服务器A上。

When I connect myself to the server A, if in the file explorer I put the link \\\\ServerB\\Documents\\General\\Administration\\Doc1.pdf , file is retrieved correctly. 当我将自己连接到服务器A时,如果在文件资源管理器中放置了\\\\ServerB\\Documents\\General\\Administration\\Doc1.pdf ,则可以正确检索文件。

Maybe it's a security problem but don't know where to search... 也许这是一个安全问题,但不知道在哪里搜索...

Thank you for your help 谢谢您的帮助

This is most likely an access rights issue. 这很可能是访问权限问题。 The folder does exist, but .NET can't determine that because the IIS user doesn't have access to the network share. 该文件夹确实存在,但是.NET无法确定,因为IIS用户无权访问网络共享。

You can check this by running the IIS application pool as an network user, for example your own account. 您可以通过以网络用户(例如您自己的帐户)身份运行IIS应用程序池来进行检查。 Don't do this in production scenarios! 不要在生产方案中这样做! Create a separate domain account for the IIS user. 为IIS用户创建一个单独的域帐户。

Try to create an Application Pool that can access the file from Server B. 尝试创建一个可以从服务器B访问文件的应用程序池。

Right-Click Application Pool > Advanced Settings

在此处输入图片说明

Go to Process Model Section > Identity > Click ApplicationPoolIdentity 转到流程模型部分>身份>单击ApplicationPoolIdentity

From Built-in account. 从内置帐户。 Select > Custom account: > Cick Set > 选择>自定义帐户:>交易集>

Set Credentials will popup. 设置凭据将弹出。 在此处输入图片说明

Set your credentials and assign the Application Pool to your application on Server A. 设置凭据并将“应用程序池”分配给服务器A上的应用程序。

Make sure the User Credential has access to Server B file. 确保用户凭证可以访问服务器B文件。

Hope this will help. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM