【发布时间】:2009-11-10 13:41:22
【问题描述】:
我有一个小的 asp.net MVC 1 Web 应用程序,它可以存储文件并在 App_Data 目录中创建目录。当写操作成功时,我向临时数据添加一条消息并执行redirectToRoute。问题是执行操作时临时数据为空。如果我将文件写入 Web 应用程序根目录之外的目录中,则临时数据不为空,并且一切正常。任何想法为什么在 app_data 中写入似乎会清除临时数据?
编辑: 如果 DRS.Logic.Repository.Manager.CreateFile(path, hpf, comment) 在 App_Data 中写入,则在重定向到的操作中 TempData 将为空。如果它是 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吗?
-
是的,我仍然使用重定向路由。编写实际文件的代码很简单,不会引发任何异常。我们尝试了redirectToAction,结果相同。
标签: asp.net asp.net-mvc tempdata appdata