ASP.net "BasePage" class ideas
<p>What cool functionality and methods do <strong><em>you</em></strong> add to your ASP.net <code>BasePage : System.Web.UI.Page</code> classes?</p>

<h2>Examples</h2>

<p>Here's something I use for authentication, and I'd like to hear your opinions on this:</p>

<pre><code>protected override void OnPreInit(EventArgs e)
{
    base.OnPreInit(e);

    // Authentication code omitted... Essentially same as below.

    if (_RequiresAuthentication &amp;&amp; !(IsAuthorized))
    {
        RespondForbidden("You do not have permissions to view this page.", UnauthorizedRedirect);
        return;
    }
}

// This function is overridden in each page subclass and fitted to each page's
// own authorization requirements.
// This also allows cascading authorization checks,
// e.g: User has permission to view page? No - base.IsAuthorized - Is user an admin?
protected virtual bool IsAuthorized
{
    get { return true; }
}
</code></pre>

<p>My BasePage class contains an instance of this class:</p>

<pre><code>public class StatusCodeResponse {

    public StatusCodeResponse(HttpContext context) {
        this._context = context;
    }

    /// &lt;summary&gt;
    /// Responds with a specified status code, and if specified - transfers to a page.
    /// &lt;/summary&gt;
    private void RespondStatusCode(HttpContext context, System.Net.HttpStatusCode status, string message, string transfer)
    {
        if (string.IsNullOrEmpty(transfer))
        {
            throw new HttpException((int)status, message);
        }

        context.Response.StatusCode = (int)status;
        context.Response.StatusDescription = message;
        context.Server.Transfer(transfer);
    }

    public void RespondForbidden(string message, string transfer)
    {
        RespondStatusCode(this._context, System.Net.HttpStatusCode.Forbidden, message, transfer);
    }

    // And a few more like these...

}
</code></pre>

<p>As a side note, this could be accomplished using extension methods for the <code>HttpResponse</code> object.</p>

<p>And another method I find quite handy for parsing querystring int arguments:</p>

<pre><code>public bool ParseId(string field, out int result)
{
    return (int.TryParse(Request.QueryString[field], out result) &amp;&amp; result &gt; 0);
}
</code></pre>
