我在我的MVC控制器操作中有以下代碼。它在運行VS2012的開發機器中運行良好,但無法保存在IIS中的生產環境中。 可能是什麼問題?我已經通過了代碼,但沒有任何錯誤。在生產中,它不會在e.Message或dbEx.Message中顯示任何錯誤。感謝您的幫助。
後,我有這應該一起保存一個文件的信息,其餘的MVC行動。
該代碼在我的開發人員機器上工作正常,但是當我在生產服務器上運行該文件時,該文件未保存。在我的控制器中我完全沒有錯誤。
這裏是我試過:
- 試圖登錄
- 我可以看到該文件被張貼在Chrome開發者工具
- 我在目標文件夾檢查文件權限的例外
代碼:
[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);
}
}
-1因爲你不解釋問題,只是希望我們解決它。你試圖解決什麼問題?你如何重現它? – jgauffin
我放在那裏的一切都是如何發生的。我該如何解釋一個我不知道的問題?這是因爲我試圖解決它,不能讓我把它放在StackOverflow上。我不是新來的,所以我知道規則。問題很簡單 - 上面的代碼片段工作正常,並且在VS 2012中運行時將MenuDto模型中的數據提交到數據庫中。即使存在斷點,也沒有任何錯誤。但是,當我將它部署在IIS上時,它不會提交到數據庫,也不是因爲它沒有連接到數據庫,因爲我已成功登錄,並且在web配置中連接字符串正常 – Charles
'但未能保存'是一個非常寬描述。什麼沒有保存?它在某些情況下工作嗎?文件是包含在帖子中還是在生產環境中缺失?你有沒有檢查文件的權限?文件夾是否存在?這就是爲什麼你得到-1。顯示一些努力。 – jgauffin