Skip navigation.

The Purpose and Objectives of a BusinessAll recent postsEdit-and-Continue Should Be an Individual Setting

Avoid Excessive Logic in Master Pages

Master pages are great at helping separate presentation from intricate logic. It’s best to place in master pages the kind of code that deals only with the look and feel of a page and its layout. Should you have additional logic, e.g. checking user credentials, persisting view state to an alternate store, etc—this code should go into a base class for other pages to inherit from.

This seems like an obvious point, but I somehow missed it today. What happens in the following code sample?

public partial class CommonMaster : MasterPage
{
  protected override void OnInit(EventArgs e)
  {
    base.OnInit(e);
    /* Suppose you run a security check here and redirect 
       the visitor to another page. */
    HttpContext.Current.Response.Redirect("~/PageB.aspx");
  }
}

Suppose, the master page performs some authorization check and bounces the user elsewhere if he/she does not have sufficient credentials. There’s risk the other page will issue the same verdict on authorization, in which case the master page goes into an infinite loop.

The lesson of the day is: master pages are good at separating concerns. Use them wisely.

Comments

No comments yet

Emails and Notifications

Would you like to be notified when somebody responds to this post? 

Submit your comment

Please enter only text since all HTML tags except hyperlinks will be stripped. Hyperlinks will become live links. Any comments with flaming or offensive language will be deleted. Be courteous to other posters. Thank you.

Your name (required):
Your email (optional):
Your site's URL (optional):
Enter this number
Type in the number above:
Comment (required):