2014-05-13 105 views
-2

之前:ASP.NET MVC項目中的未知錯誤。

我在我的MVC控制器操作中有以下代碼。它在運行VS2012的開發機器中運行良好,但無法保存在IIS中的生產環境中。 可能是什麼問題?我已經通過了代碼,但沒有任何錯誤。在生產中,它不會在e.Message或dbEx.Message中顯示任何錯誤。感謝您的幫助。

後,我有這應該一起保存一個文件的信息,其餘的MVC行動。

該代碼在我的開發人員機器上工作正常,但是當我在生產服務器上運行該文件時,該文件未保存。在我的控制器中我完全沒有錯誤。

這裏是我試過:

  1. 試圖登錄
  2. 我可以看到該文件被張貼在Chrome開發者工具
  3. 我在目標文件夾檢查文件權限的例外

代碼:

[HttpPost] 
    public ActionResult CreateRecipe(MenuDto model, HttpPostedFileBase file) 
    { 

     if (Session["username"] == null || Session["jobtitle"] == null) 
     { 
      TempData["msg"] = "Sorry your session has timed out or you have not logged in. Please log-in."; 
      return RedirectToAction("LogIn", "Home"); 
     } 
     try 
     { 

      var cl = new CategoryBL(); 
     ViewBag.ListCategories = new SelectList(new List<Category>(){ new Category() { CatName = "--Select Category--"}}.Concat(cl.RetrieveAll()), 
      "CatName", "CatName"); 

      RecipeBL bl = new RecipeBL(); 
      model.Recipes = bl.RetrieveAll(); 
      if (file == null) 
      { 
       ViewBag.Msg = "Please select a picture for this recipe"; 
       return View(model); 
      } 

      string path = ""; 

      if (ModelState.IsValid) 
      { 
       if (file != null) 
       { 
        string pic = System.IO.Path.GetFileName(file.FileName); 
        path = System.IO.Path.Combine(Server.MapPath("~/pictures/Recipes"), pic); 

        // file is uploaded 
        file.SaveAs(path); 

        model.Image = @"\pictures\Recipes\" + file.FileName; 
       } 


       var item = new Recipe() 
       { 
        RecipeCode = model.RecipeCode, 
        RecipeMethod = model.RecipeMethod, 
        RecipeName = model.RecipeName, 
        SellingPrice = model.SellingPrice, 
        Image = model.Image, 
        SubCategory = model.SubCategory 
       }; 
       bl.Create(item, Session["username"].ToString()); 

      } 
      ViewBag.Msg = "Recipe was successfully registered."; 
      model.Recipes = bl.RetrieveAll(); 
      return View(model); 
     } 
     catch (DbEntityValidationException dbEx) 
     { 
      foreach (var validationErrors in dbEx.EntityValidationErrors) 
      { 
       foreach (var validationError in validationErrors.ValidationErrors) 
       { 
        Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); 
       } 
      } 

      ViewBag.Msg = "Recipe registration failed." + dbEx.Message; 
      return View(model); 
     } 
     catch (Exception e) 
     { 
      ViewBag.Msg = "Recipe registration failed." + e.Message; 
      return View(model); 
     } 
    } 
+0

-1因爲你不解釋問題,只是希望我們解決它。你試圖解決什麼問題?你如何重現它? – jgauffin

+0

我放在那裏的一切都是如何發生的。我該如何解釋一個我不知道的問題?這是因爲我試圖解決它,不能讓我把它放在StackOverflow上。我不是新來的,所以我知道規則。問題很簡單 - 上面的代碼片段工作正常,並且在VS 2012中運行時將MenuDto模型中的數據提交到數據庫中。即使存在斷點,也沒有任何錯誤。但是,當我將它部署在IIS上時,它不會提交到數據庫,也不是因爲它沒有連接到數據庫,因爲我已成功登錄,並且在web配置中連接字符串正常 – Charles

+1

'但未能保存'是一個非常寬描述。什麼沒有保存?它在某些情況下工作嗎?文件是包含在帖子中還是在生產環境中缺失?你有沒有檢查文件的權限?文件夾是否存在?這就是爲什麼你得到-1。顯示一些努力。 – jgauffin

回答

0

最後,出現錯誤。這是一個拒絕訪問被拒絕的錯誤 - 將文件上傳到C:\ users \ inetpub ...。\ picturefolder \失敗。拒絕訪問。我意識到錯誤是隱藏的,但我不知道爲什麼。它最終在今天展現出來。

很簡單的解決方案是更改登錄用戶的文件夾的安全/文件夾權限設置。我右鍵單擊inetpub父文件夾並在上下文菜單中選擇屬性,然後選擇安全選項卡。當我點擊登錄的用戶時,我注意到,「權限」窗口中的所有框都未在「允許」下進行檢查。所以我選擇了ALL(寫入等),然後是Applied。我現在很開心。我試圖通過操作來保存上傳的圖像文件,並按照原樣運行。 :)

+1

更改權限就足夠了。 – jgauffin

相關問題