views:

274

answers:

2

I have a small asp.net MVC 1 web app that can store files and create directories in the App_Data directory. When the write operation succeeds, I add a message to the tempdata and do a redirectToRoute. The problem is that the tempdata is null when the action is executed. If i write the files in a directory outside of the web applications root directory, the tempdata is not null and everything works correctly. Any ideas why writing in the app_data seems to clear the tempdata ?

edit: if DRS.Logic.Repository.Manager.CreateFile(path, hpf, comment) writes in the App_Data, TempData will be null in the action being redirected to. if it is a directory out of the web app root it is fine. No exceptions are being thrown.

[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();
    }
}
A: 

I would suggest you, don't store your files in the App_data folder because this folder is a special folder reserved for data files such as database (.mdb) files and so on, and this is by design. This might be the case you are getting tempdata is null

you need to put your files outside of App_Data folder.

Muhammad Akhtar
I am aware that the App_Data is "special", it has been useful in previous projects because it allows our app to write user uploaded files in a directory that is located under the website root without exposing the files directly. I could store the files in a directory outside of the website root, but then I must add extra configurations and deal with IT personel. Just curious about the effects on the tempdata.
RAMX
+1  A: 

Figured out what was causing tempdata to become null. DRS.Logic.Repository.Manager.CreateFile(path, hpf, comment); creates a temp directory under ~/App_Data/, writes a file in that directory, commits that file to a repository and then cleans up the temp directory. It seems that certain io operations within App_Data trigger the filesystem monitor and the web application is restarted. I was using an inproc session so when the application would restart, the session would be cleared. Tempdata is actually stored in the session so it was cleared as well. solution: dont use inproc session or store files outside of the web application's root directory. I had no idea that changes under App_data triggered an application restart.

RAMX
Huh. You say writing to app_data caused the application restart? What are the certain IO operations? It would be interesting to see what ASP.Net actually reported: http://weblogs.asp.net/scottgu/archive/2005/12/14/433194.aspx
AndrewDotHay
It seems that the IO operation causing the restart is deleting a folder.
jhexp