tags:

views:

114

answers:

2

Hi there,

I wonder if anyone can help, i wish to save a variable globally for the current user on the current page, i thought Sessions are bad in mvc??

I should give an example of what i am doing. Basically i have a controller and it enters (Action = Index) and i check

Request.UrlReferrer

And if it contains a value that means it arrived from another place i.e. Google for example. And i write the information to a log. Now at this stage in the controller all is fine.

But later in my page i wish to recall a method in the controller (using jquery and ajax) to process some more tracking but remembering i am still in the same page, i use

 Request.UrlReferrer

but at this stage its always going to be the name of the page as the referrer is the page that initiated the ajax call and "NOT" google. Ok so it appears i need to save the value or UrlReferrer to a global variable/per user so that when i reenter my controller i can check this SAVED variable rather thatn Request.UrlReferrer.

Does anyone know the easiest way to do this?

here is an example of my page

public ActionResult Index()
    {
        // Process tracking - Initial entry
        string ip = Request.UserHostAddress.ToString();
        string referrer = null;

        if (Request.UrlReferrer !=null)
            referrer = Request.UrlReferrer.ToString();

        // WRITE THE LOGS HERE!!!!

        return View();
    }

    public ActionResult ProcessTracking()
    {
        // Reprocess tracking
        // BUt can't use Request.UrlReferrer as it returns my calling page and not google
        // for example




        //string ip = Request.UserHostAddress.ToString();
        //string referrer = null;

        //if (Request.UrlReferrer != null)
        //    referrer = Request.UrlReferrer.ToString();

        //return View();
    }
A: 

Hi There,

It's ok to use the session bag to store the first value of Request.UrlReferrer. But as in WebForms, be sure that you'll remove the session variable as it become unused. Another options for this persistance can be cookies or the ASP.NET MVC TempData bag that in MVC 1.0 can only be used before you redirect to another page and at MVC 2.0 Beta TempData is cleared only when it is read (or when the session expires).

Lucas Oleiro

Oleiro
+2  A: 

You should store the value in TempData. The whole purpose of TempData is to persist information between controller calls. As of MVC 2 Beta the value will stay there until it is read.

public ActionResult Index()
{        
// Process tracking - Initial entry        
string ip = Request.UserHostAddress.ToString();        
string referrer = null;        
if (Request.UrlReferrer !=null)            
referrer = Request.UrlReferrer.ToString(); 
TempData["referrer"] = referrer;   
// WRITE THE LOGS HERE!!!!        
return View();    
}    

public ActionResult ProcessTracking()    
{        
// Reprocess tracking        
// BUt can't use Request.UrlReferrer as it returns my calling page and not google        
// for example        
string ip = Request.UserHostAddress.ToString();        
string referrer = TempData["referrer"];        
//continue processing here   
return View();    
}
Jeff French
Thanks, yes this appears exactly what i want! I just tested it and it appears to work but i have a question. Is it left there until i read it in ASPNET MVC 1.0 as well?? or not? .. and i presume that its not a global variable hence if i have 2 users the variable is not going to overwrite the other?
mark smith
The TempData is scoped in the context of the request so it will not be overwritten by simultaneous users. In MVC 1.0 the TempData will be cleared on the next controller call. That means that if there is a controller action called (including ajax calls) the data will only be available on the VERY NEXT call. If you need the data accross more calls, you need to have each action "touch" the data:TempData["referrer"] = TempData["referrer"];A little hacky? Yes, but the MVC team has fixed this for MVC 2 so it will get better!
Jeff French
wow Jeff, thanks.. just want i needed!
mark smith
No, problem! Glad I could help. :)
Jeff French