MVC Equivalent of Page_Load

10,486

Solution 1

First, you should redirect to Home, not return the Home View, otherwise you have the weird situation of the home page showing up despite the Url being somewhere else.

Second, Session will never be Null, because a new session gets created when the old one expires or is reset. You would instead check for your variable and if THAT is null, then you know the session is new.

Third, If you app depends on this session data, then I would not use a session at all. Are you using this to cache data? If so, then using Cache may be a better choice (your app gets notified when cache items expire).

Unfortunately, this is probably a case of The XY Problem. You have a problem, and you believe Session solves your problem, but you're running into a different problem with Session, so you are asking how to solve your session problem rather than how to solve the problem Session is trying to solve.

What is the real problem you are trying to solve with this?

EDIT:

Based on your comment below, why don't you pass the customer number on the url:

http://website/Controller/ViewRecord/3

public ActionResult ViewRecord(int id) 
{ 
    // do whatever you need to do with the customer ID
} 

Solution 2

If it's in one controller, you can do this:

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
    base.OnActionExecuting(filterContext);
    ... // do your magic
}   

It will fire before on any action execution. You can't return a view from there though, you'll have to redirect to anything that returns action result, e.g:

filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary { { "controller", "Shared" }, { "action", "Home" } });

But, obviously, that should redirect to the action in the controller that's not affected by the override, otherwise you have a circular redirect. :)

Share:
10,486
Turp
Author by

Turp

I am a Software Developer for VisionMenu.

Updated on June 09, 2022

Comments

  • Turp
    Turp almost 2 years

    I have a session variable that is set in my MVC application. Whenever that session expires and the user tries to refresh the page that they're on, the page will throw an error because the session is not set anymore.

    Is there anywhere I can check to see if the session is set before loading a view? Perhaps putting something inside the Global.asax file?

    I could do something like this at the beginning of EVERY ActionResult.

    public ActionResult ViewRecord()
    {
        if (MyClass.SessionName == null)
        {
            return View("Home");
        }
        else
        {
            //do something with the session variable
        }
    }
    

    Is there any alternative to doing this? What would the best practice be in this case?

  • Turp
    Turp about 12 years
    The Web Application allows to search for a customer. We select a customer based on their ID Number. I am setting that ID Number has a Session. Here is where I do that at: ` public static string CIF { get { if (HttpContext.Current.Session["CIF"] == null) return ""; else return HttpContext.Current.Session["CIF"].ToString(); } set { HttpContext.Current.Session["CIF"] = value; } }` -- Then on a View, I am calling MyClass.CIF to get the value of the Session.
  • Erik Funkenbusch
    Erik Funkenbusch about 12 years
    @Turp - Why do you need to add the customer to the session? Why not just pass the customer number in the URL? Then the value is not stored in the session and it goes away when you don't need it anymore.
  • Turp
    Turp about 12 years
    We really have no actual reason, except that's the way it was done before me in the original version of the web app. Do you mean something along the lines of /Customer/Edit/1234? We had discussed that it doesn't look as good as /CustomerProfile/Edit and another thing that was brought up was that we just didn't want our users to replace the current number with a random number to pull up another of our customers. BTY, this is an internal web application only. Where do you stand on this?
  • Erik Funkenbusch
    Erik Funkenbusch about 12 years
    @Turp - That is contrary to what you told me above. You said a user could search for a customer, that means a customer other than themselves, otherwise search is pointless. Second, you would have to include some security in the lookup to prevent accessing records that are not allowed. But, because you're changing the story here, it's confusing as to what you are actually doing.
  • Erik Funkenbusch
    Erik Funkenbusch about 12 years
    @Turp - The other issue, of just wanting a URL of /CustomerProfile/Edit then you would lookup the customers customer id based on their login credentials and use it in the query, it doesn't need to be stored in a session.
  • Turp
    Turp about 12 years
    Sorry for the confusion with this. This is an internal app. When I said "User" I was referring to us as the company. We would be looking up Customers in this application. And now that we have discussed, I will use the /Customer/Edit/1234 way of pulling up a customer.