首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FileNotFoundException IIS7

FileNotFoundException IIS7
EN

Stack Overflow用户
提问于 2012-02-14 07:22:18
回答 3查看 2.7K关注 0票数 3

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

这段代码本质上是抓取网络共享位置上的图像的缩略图,对其进行操作,然后将其推送回网页。

web服务器与其尝试访问的文件位于同一网络中。所有访问此网页的用户都在同一个本地intranet上。

该应用程序是以某种方式分类的网络保存的资源的存储列表。

当我部署应用程序时,它给出了上面两个我在应用程序日志中发现的错误。我觉得这是一个文件权限错误,这两个错误是联系在一起的,但我不知道在哪里更改权限才能使应用程序正常工作。

代码语言:javascript
复制
Exception information: 
Exception type: FileNotFoundException 
Exception message: T:\Published\Generic.jpg

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

处理图像的代码部分如下:

代码语言:javascript
复制
System.Drawing.Image img;
img = System.Drawing.Image.FromFile(MapPath(Request.QueryString["File"].ToString()));

我在使用和不使用MapPath方法的情况下都尝试过。

我尝试调试应用程序,但是因为它在VS2010中工作,所以没有抛出异常,所以我不知道为什么它会被抛到IIS上。

请求的整个堆栈跟踪:

代码语言:javascript
复制
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的内容:

代码语言:javascript
复制
 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();
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-02-14 07:41:36

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

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

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

要启用模拟,您可以转到应用程序,选择身份验证功能,启用ASP.NET模拟,然后单击编辑。并确保选择了经过身份验证的用户。模拟还可以通过在最后一个对话框中使用特定用户来处理特定的用户标识,但当您想要在通常不能作为服务运行的用户的上下文中运行时,这主要是有用的。

编辑:

显然,IIS AppPool用户在机器上下文DOMAIN\Machine$下运行。参见Application Pool Identities

票数 3
EN

Stack Overflow用户

发布于 2012-02-14 07:28:23

IIS7辅助进程使用自己的凭据运行。它将以运行应用程序池的身份访问该文件,您的网站在该应用程序池下运行。这通常是ApplicationPoolIdentityNetworkService。您需要授予该用户对相关文件的访问权限。

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

票数 2
EN

Stack Overflow用户

发布于 2012-02-14 07:39:44

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9269570

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档