asp.netserver.transferresponse.addheader

How to make a server.transfer() with a Response.AddHeader("refresh", "seconds") and not get a 404 error?


so this is the scenario:

I have a base class for all login-controlled pages:

public class SessionControlledPage : Page
{
    protected virtual void Page_Load(Object sender, EventArgs e)
    {
        Response.AddHeader("Refresh", Convert.ToString(Session.Timeout * 60 + 5));
        if (Session.IsNewSession)
        {
            Response.Redirect("~/login.aspx");
        }
    }
}

And a regularpage.aspx page that inherints from the base class:

public partial class RegularPage : SessionControlledPage
{
    override protected void Page_Load(Object sender, EventArgs e)
    {
        base.Page_Load(sender, e);

        Server.Transfer("~/otherpage.aspx");
    }
}

Now let's say the original URL was http://localhost/regularpage.aspx and that Session.Timeout = 5. The problem appears when the refresh happens after the transfer takes place. I'm getting a 404 Not Found error and the URL changes for http://localhost/305. Notice that 5 * 60 + 5 = 305.

Do you know why is this happening? got a solution?

PD: I use transfer because I need to access some properties of regularpage.aspx on otherpage.aspx, maybe you could provide another solution for this too.


Solution

  • There is very little reason to call base.Page_Load if all you're going to do is then call Server.Transfer. What exactly are you trying to accomplish? If you're just accessing some properties you need to abstract this into some business logic that does not rely on the Page object.

    This is also what is causing the 404 issue... for this to happen, the problem has to be in the rendered output of the page (check it out). It seems like you are cutting off one page right after the meta-refresh tag and then starting a new page and ASP.NET is just dumping it all into the same response stream. In short, you're doing it wrong. :) You might be able to fix this with a well-placed Response.Clear(), but that's not the real problem here... and you'd lose your refresh tag.