简体   繁体   English

Page_Load 的 MVC 等价物

[英]MVC Equivalent of Page_Load

I have a session variable that is set in my MVC application.我的 MVC 应用程序中设置了一个 session 变量。 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.每当 session 过期并且用户尝试刷新他们所在的页面时,页面将抛出错误,因为 session 不再设置。

Is there anywhere I can check to see if the session is set before loading a view?在加载视图之前,有什么地方可以检查 session 是否已设置? Perhaps putting something inside the Global.asax file?也许在 Global.asax 文件中放一些东西?

I could do something like this at the beginning of EVERY ActionResult .我可以在每个 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?在这种情况下,最佳做法是什么?

If it's in one controller, you can do this:如果它在一个 controller 中,您可以这样做:

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, eg:你不能从那里返回一个视图,你必须重定向到任何返回操作结果的东西,例如:

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.但是,显然,这应该重定向到不受覆盖影响的 controller 中的操作,否则您将进行循环重定向。 :) :)

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.首先,您应该重定向到主页,而不是返回主页视图,否则尽管 Url 在其他地方,您仍会出现主页的奇怪情况。

Second, Session will never be Null, because a new session gets created when the old one expires or is reset.其次,Session 永远不会是 Null,因为当旧的过期或重置时会创建一个新的 session。 You would instead check for your variable and if THAT is null, then you know the session is new.你会检查你的变量,如果它是 null,那么你知道 session 是新的。

Third, If you app depends on this session data, then I would not use a session at all.第三,如果你的应用程序依赖于这个 session 数据,那么我根本不会使用 session。 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 .不幸的是,这可能是XY 问题的一个例子。 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.你有问题,你相信 Session 解决了你的问题,但你遇到了 Session 的不同问题,所以你问的是如何解决你的 session 问题,而不是如何解决 Session 试图解决的问题。

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:根据您在下面的评论,您为什么不通过 url 上的客户编号:

http://website/Controller/ViewRecord/3

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM