我有一個小型的asp.net MVC 1 web應用程序,可以存儲文件並在App_Data目錄中創建目錄。當寫入操作成功時,我向tempdata添加一條消息並執行redirectToRoute。問題是執行操作時tempdata爲空。如果我將這些文件寫入Web應用程序根目錄之外的目錄中,則tempdata不爲null,並且一切正常。任何想法爲什麼寫在app_data似乎清除tempdata?在App_Data中寫入文件會導致tempdata爲空
編輯: 如果DRS.Logic.Repository.Manager.CreateFile(path,hpf,comment)在App_Data中寫入,則TempData在被重定向到的操作中將爲null。如果它是一個不在Web應用程序根目錄中的目錄,那很好。沒有例外被拋出。
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(int id, string path, FormCollection form)
{
ViewData["path"] = path;
ViewData["id"] = id;
HttpPostedFileBase hpf;
string comment = form["FileComment"];
hpf = Request.Files["File"] as HttpPostedFileBase;
if (hpf.ContentLength != 0)
{
DRS.Logic.Repository.Manager.CreateFile(path, hpf, comment);
TempData["notification"] = "file was created";
return RedirectToRoute(new { controller = "File", action ="ViewDetails", id = id, path = path + Path.GetFileName(hpf.FileName) });
}
else
{
TempData["notification"] = "No file were selected.";
return View();
}
}
請發佈代碼,當您寫入不同的目錄時,您還在使用RedirectToRoute嗎? – Lazarus 2009-11-10 13:44:33
是的,我仍然使用redirecttoroute。編寫實際文件的代碼很簡單,並且不會拋出任何異常。我們用相同的結果嘗試了redirectToAction。 – RAMX 2009-11-10 14:24:25