A Base page in ASP.NET

If you want to override the way something in ASP.NET works, it can be more efficient to build it into a base class rather than including the code in every page. Two specific instances where I've done this are:

IsPostback
Little-known fact: it's quite feasible to craft a request that, to ASP.NET, looks like a postback but is submitted with a GET request. Who does this? Hackers, that's who. A call to IsPostback in this case will return true, but it shoud really return false. To get round this, build a base class that overrides IsPostBack:

Public Class MyBase
    Inherits System.Web.UI.Page
    <DebuggerStepThrough()> _
    Public Shadows Function IsPostback() As Boolean

        'Check the built-in IsPostback and make sure this is a HTTP POST
        Return (Page.IsPostBack AndAlso Request.HttpMethod.ToUpper = "POST")

    End Function

End Class

Error Handling
In Beginning ASP.NET Security, Blowdart talks about the fact that if you use ASP.NET's error handling to redirect the client to a custom error page, hackers (again) can detect the redirection and flag it as an error that may be exploitable. A more secure pattern is to handle the Page's Error event and do a Server.Transfer (which doesn't send anything to the client). Again, doing this in a base class means you only write the code once:

public partial MyBase : System.Web.UI.Page
{
    protected void Page_Error (object sender, EventArgs e)
    {
        Exception ex = Server.GetLastError();

        // Do something with the exception e.g. log it
        ...

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

Yes, I do.

But please remember that the purpose of a base page is totally different from the purpose of a master page.

Let me explain.

Master pages

are layout elements used to share the same graphical features and part of the webforms behaviour (think a login/logout box with code-behind) across all pages that are associated to the master. Your final page classes will include a reference to the master page so the final result will appear as the master page including your page (check the source code to tell who contains whom)

Base pages

are (abstract? at least not sealed!) classes from which all your pages inherit from the code-behind view. Unless you explicitly and programmatically add controls to the basae page, ie. in the constructor via LoadControl method, all pages will look blank from the very beginning until you add code.

But often they are useful. If you want to override some of the base class methods, you can have the overriden behaviour shared across all pages. Or, you may want to expose application-specific objects to the children pages (a reference to a data access layer, a logger or whatever). An example is overriding UICulture property to retrieve the user-preferred language from cookies.

Both can be combined

Depending on your goals, you may combine master pages with base pages.

I suggest you to always create a base page class, since if your application's requirements change over time and you already created lots of pages, you can try to modify the base class to have the modifications propagated to all pages, according to the level of complexity of them.

Tags:

Asp.Net