You may find following snippet very handy in situations where you want to incorporate a base page in a project that uses a master page. From an architectural point of view, implementing a base page is something you should always consider. If you need to implement something in your content pages at a later stage, you only need to do it once, in your base page class. Personally I use it to add meta content and description tags to each content page in my "Web Application Project" site - think SEO!

Refer to Jim Azar's great article for the base page code for adding the meta tags. Note also that you can add your own tags to the the Page attribute of your base class.

In the Page tag of your content page, pay particular attention to the use of the CodeFileBaseClass attribute as this is used to access the public properties of your base page class. This, along with the CodeFile tag, are the new additions to the Page tag for this particular scenario - if you were not using a base page, you wouldn't have these two attributes; instead, you would just have the usual CodeBehind tag.

Master Page:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="MyProject.master.cs"
    Inherits="MyProject.MasterPages.MyProject" %>

<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">   
</asp:ContentPlaceHolder>

 

Base Page:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPages/MyProject.Master"
    AutoEventWireup="true" CodeBehind="BasePage.aspx.cs"
        Inherits="MyProject.BasePage" %>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1"
    runat="server">
</asp:Content>

 

Content Page:

<%@ Page Title="MyProject - Home" Language="C#"
    MasterPageFile="~/MasterPages/MyProject.Master" AutoEventWireup="true"
        CodeFileBaseClass="MyProject.BasePage" CodeFile="Default.aspx.cs"
            Inherits="MyProject.Default"
                Meta_Description="Code Snippet: Master Page and Base Page"
                    Meta_Keywords="master, base, content" Theme="Style" %>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1"
   runat="server">
</asp:Content>

 




HTML Markup I don't know about you, but as a Web Developer I sometimes find myself not doing the things I should be doing. The recent rash of SQL Injection script attacks have shown me personally how little thought I sometimes give to where the input is coming from when I'm developing forms.

Then there is the generated markup itself. All our development efforts end up in plain HTML being spit out onto a'page' on a device of some kind. When you consider how sensitive the search bots are to the structure, content and placement of our markup, isn't it amazing that the only time we ever think of it is when we have to generate it dynamically in some code-behind? :-O

If you are using master pages, then you won't want to have duplicate content, in the shape of similar meta tag content, all over your site. So, you will have to generate the meta tags programmatically in the code-behind of the content pages:

HtmlHead head = this.Master.Page.Header;
HtmlMeta meta = new HtmlMeta();
meta.Name = "Description";
meta.Content = "Friendly and relevant content";
head.Controls.Add(meta);

 

You also do not want pages in the secure area of your site to be spidered:

HtmlHead head = this.Master.Page.Header;
HtmlMeta meta = new HtmlMeta();
meta.Name = "googlebot";
meta.Content = "noindex, nofollow";
head.Controls.Add(meta);

 

I recently noticed a big chunk of JavaScript in the markup that I was using to solve the problem of pushing the footer to the bottom of the page, and not relying on copious amounts of needless content and spacers to do the job that CSS cannot. I should have had it in an external file and used the appropiate ASP.NET method to register it and pull it in. So, I created a JS file called footerFix.js, placed it in its own folder and used the following in the master page code-behind:

string myScript = "/js/footerFix.js";
Page.ClientScript.RegisterClientScriptInclude("myKey", myScript);

 

This created the following markup in my page:

<script src="/js/footerFix.js" type="text/javascript"></script>

 

Now I have a smaller page, faster download time and a fairly good chance of the spiders actually getting the info they require.