[英]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中工作,它不會拋出異常所以我不知道為什么它被扔在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身份驗證)。
您可以通過右鍵單擊應用程序池並選擇高級設置來更改應用程序池標識。 過程模型下的身份是要改變的設置。
要啟用模擬,您可以轉到應用程序,選擇身份驗證功能,啟用ASP.NET模擬,然后單擊編輯..並確保選中了身份驗證用戶。 模擬也可以通過在最后一個對話框中使用特定用戶來使用特定用戶身份,但是當您希望在通常無法作為服務運行的用戶的上下文中運行時,這非常有用。
編輯:
顯然,IIS AppPool用戶在機器上下文中運行,即DOMAIN\\Machine$
。 請參閱應用程序池標識 。
IIS7工作進程在其自己的憑據下運行。 它將以運行運行您網站的應用程序池的標識來訪問該文件。 這通常是ApplicationPoolIdentity
或NetworkService
。 您需要授予該用戶對相關文件的訪問權限。
但是如果你真的得到一個可能不是你的問題的FileNotFoundException
,那么請發布整個堆棧跟蹤。
我認為這是因為您使用映射的驅動器名稱訪問圖像。 相反,如果在IIS虛擬目錄中使用T:\\ Published \\ Generic.jpg,請嘗試使用UNC name \\ machineName \\ Published \\ Generic.jpg
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.