FileNotFoundException IIS7

我有一个在VS2010中运行完美的C#Web应用程序,但是当部署到IIS7服务器时,返回“图像未找到图标”。

有问题的代码基本上抓取网络共享位置上的图像缩略图,进行操作,然后推回到网页。

Web服务器与它尝试访问的文件位于同一网络上。 访问此网页的所有用户都在同一本地Intranet上。

该应用程序是存储的网络节省资源列表,这些资源以这种或那种方式分类。

部署应用程序时,它会在我的应用程序日志中找到上述两个错误。 我觉得这是一个文件权限错误,并且两个错误是链接的,但我不知道在哪里更改权限以使应用程序正常工作。

Exception information: Exception type: FileNotFoundException Exception message: T:\Published\Generic.jpg 

但是,如果我拿“T:\ Published \ Generic.jpg”并将其插入我的IE地址栏。 它加载图像。

处理图像的代码部分是这样的:

 System.Drawing.Image img; img = System.Drawing.Image.FromFile(MapPath(Request.QueryString["File"].ToString())); 

我已经尝试过使用和不使用MapPath方法。

我尝试调试应用程序,但因为它在VS2010中工作,它不会抛出exception所以我不知道为什么它被扔在IIS服务器上。

整个堆栈跟踪请求:

 Event code: 3005 Event message: An unhandled exception has occurred. Event time: 13/02/2012 4:16:26 PM Event time (UTC): 13/02/2012 11:16:26 PM Event ID: 1f01693f71a2443790a8d83ba06a88a4 Event sequence: 12 Event occurrence: 1 Event detail code: 0 Application information: Application domain: /LM/W3SVC/2/ROOT-3-129736485835718008 Trust level: Full Application Virtual Path: / Application Path: C:\inetpub\wwwroot\ Machine name: XXXXXX Process information: Process ID: 10768 Process name: w3wp.exe Account name: IIS APPPOOL\ASP.NET v4.0 Exception information: Exception type: FileNotFoundException Exception message: T:\Published\Generic.jpg at System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement) at imagedrawer.Page_Load(Object sender, EventArgs e) at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) Request information: Request URL: http://localhost/imagedrawer.aspx?File=T:\Published\Generic.jpg Request path: /imagedrawer.aspx User host address: ::1 User: Is authenticated: False Authentication Type: Thread account name: IIS APPPOOL\ASP.NET v4.0 Thread information: Thread ID: 64 Thread account name: IIS APPPOOL\ASP.NET v4.0 Is impersonating: False Stack trace: at System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement) at imagedrawer.Page_Load(Object sender, EventArgs e) at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) Custom event details: 

imagedrawer.aspx的内容:

  System.IO.MemoryStream ms = new System.IO.MemoryStream(); System.Drawing.Image img; img = System.Drawing.Image.FromFile(MapPath(Request.QueryString["File"].ToString())); if (img.Height > 80 || img.Width > 80) { System.Drawing.RectangleF RF = new System.Drawing.RectangleF(); RF.X = 0; RF.Y = 0; RF.Height = (img.Height < 80) ? img.Height : 80; RF.Width = (img.Width < 80) ? img.Width : 80; System.Drawing.Bitmap bmthumb = (System.Drawing.Bitmap)img.Clone(); System.Drawing.Bitmap bmCrop = bmthumb.Clone(RF, bmthumb.PixelFormat); img = (System.Drawing.Image)bmCrop; } img.Save(ms, System.Drawing.Imaging.ImageFormat.Png); Response.Clear(); Response.AddHeader("Content-Disposition", "attachment; filename=" + Request.QueryString["File"].ToString()); Response.AddHeader("Content-Length", ms.ToArray().Length.ToString()); Response.ContentType = "image/jpeg"; Response.BinaryWrite(ms.ToArray()); Response.End(); img.Dispose(); 

我不认为网络驱动器在服务环境下可用。 您可能必须使用网络共享表示法(例如\\machine-name\share )。 此外,您在默认用户上下文( IIS APPPOOL\ASP.NET v4.0 )下运行,这更难以在网络设置中工作。 您应该将应用程序池标识更改为网络用户,并授予该用户访问权限。

另一种选择是模拟访问应用程序的用户(假设您使用的是Windows身份validation)。

您可以通过右键单击应用程序池并选择高级设置来更改应用程序池标识。 过程模型下的身份是要改变的设置。

要启用模拟,您可以转到应用程序,选择身份validationfunction,启用ASP.NET模拟,然后单击编辑..并确保选中了身份validation用户。 通过在最后一个对话框中使用“特定用户”,模拟也可以使用特定用户标识,但是当您希望在通常无法作为服务运行的用户的上下文中运行时,这非常有用。

编辑:

显然,IIS AppPool用户在机器上下文中运行,即DOMAIN\Machine$ 。 请参阅应用程序池标识 。

IIS7工作进程在其自己的凭据下运行。 它将以运行运行您网站的应用程序池的标识的forms访问该文件。 这通常是ApplicationPoolIdentityNetworkService 。 您需要授予该用户对相关文件的访问权限。

但是如果你真的得到一个可能不是你的问题的FileNotFoundException ,那么请发布整个堆栈跟踪。

我认为这是因为您使用映射的驱动器名称访问图像。 相反,如果在IIS虚拟目录中使用T:\ Published \ Generic.jpg,请尝试UNC name \ machineName \ Published \ Generic.jpg