Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A Base page in ASP.NET

Tags:

asp.net

Do you recommend from every web site created in Visual Studio, that you should create a Base page that serves as the parent class?

What are the exact benefits/drawbacks?

like image 886
TeaDrinkingGeek Avatar asked Feb 18 '11 13:02

TeaDrinkingGeek


3 Answers

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");
     }
}
like image 180
PhilPursglove Avatar answered Nov 17 '22 15:11

PhilPursglove


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.

like image 30
usr-local-ΕΨΗΕΛΩΝ Avatar answered Nov 17 '22 16:11

usr-local-ΕΨΗΕΛΩΝ


Check out masterpages this is their primary purpose.

Here's a link: http://msdn.microsoft.com/en-us/library/wtxbf3hh.aspx

This will serve as the template for your site. You would add a content section that would make up the body of your site. You can reference the master page is your subpages to have a consistent layout, menu, etc. for you site.

Also, like the others have noted. If you are running any commond code, just create a class a reference it from wherever you need it.

like image 1
Matt Avatar answered Nov 17 '22 16:11

Matt