Prevent page life cycle?

余生长醉 提交于 2019-12-24 08:55:43

问题


I have a master page with a logout link on it. When the user clicks the link I call the following method:

    private void Logout()
    {
        SessionBll.DeleteSessionEntry(this.sessionId);
        FormsAuthentication.SignOut();
        Roles.DeleteCookie();
        LogHelper.Location = string.Empty;

        LogHelper.Info(logger, HttpContext.Current.User.Identity.Name, 
           MethodBase.GetCurrentMethod().Name, "User has been logged out from 
                Administrator.Master", LogMessage.LoggedOut);
        FormsAuthentication.RedirectToLoginPage();
    }

Now, when a user is on a page and they click the link, the normal page cycle happens and this method is called.

The problem with this is that the user shouldn't have to wait for the page cycle, they should be taken to the logout page as fast as possible (as long as it takes to run the logout method on the server).

How do I skip the page life cycle, yet still call this method?

Thanks.


回答1:


You can't skip the page life cycle; what you can do instead is this:

Change that logout link to a normal link (<a href="...">) and on Page_Load of the Login page, execute that code you posted but only if(!IsPostBack)

Edit

If your Login page needs to know (as you put it) when to execute your cleanup code; you can pass a parameter on the logout link as so:

<a href="Login.aspx?Logout=y" > Logout </a>

And then check on Page_Load:

if (!IsPostBack &&  Request.QueryString["Logout"]=="Y" )
{
   //Your code here
}

This is super fast.




回答2:


The problem with this is that the user shouldn't have to wait for the page cycle

I would argue that the problem is that the page is apparently doing too much in the page cycle. In WebForms, the page cycle happens. It's just how it works. Consider this...

  • In order to interact with the code-behind, the client-side form needs to POST to the page.
  • In order to handle the POST, the page needs to exist in memory.
  • In order to exist, the page needs to go through its standard creation steps in its life cycle.

Without seeing any actual code, my gut reaction is that you're doing too much on Page_Load. Refactor it out. Only do what you need to do to load the page. Not to process data, not to do all kinds of back-end work, just load the page. This should be a fairly light process. Then logically plug in your necessary background work where appropriate. (In WebForms, and this is a major pet peeve of mine, this often ends up as wrapping a lot of stuff in a conditional to check IsPostBack.)

Now, there is something you can do. Based on your question, it sounds like what you want to skip is the loading of the current page, and instead go straight to the logout page. Depending on your setup, you have a couple of approaches:

  • Make the logout link nothing more than a simple link. That way the browser will only request the logout page, as opposed to making a form POST to the current page (as it does with, say, a button or a LinkButton).
  • If the logout page actually needs some kind of value POSTed to it, make it its own HTML form with the action set to the logout page. This involves relying less on the server-side drag-and-drop controls and more on just crafting some simple HTML (which is always a good thing to be able to do in web development).

Or, thinking about your question a bit more, are you asking how to make the logout page skip the Master Page's cycle? If the logout page uses the Master Page, then it can't skip it. The page has to exist before it can be used. But you can create a standalone logout page without a Master Page which does nothing more than process the logout (and then redirect, I suppose, to another page). This would process the logout quickly, but overall still require that a page be loaded somewhere. Which brings us back to the point that the problem is that the page takes too long to load, not that you're loading a page.




回答3:


Use something that doesn't have the lifecycle - ASP.NET MVC as someone suggested or create an .ashx (handler): logout.ashx would simply log out the logged-in user. That would work without having to change frameworks.

This is not often done, though, usually you'd use a different page, which would still have a full lifecycle (as per Icarus' answer).




回答4:


Call entirely different page that would only perform Logout task an no other relevant code. That way, you don't have to worry about page life cycle.



来源:https://stackoverflow.com/questions/8671032/prevent-page-life-cycle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!