My master page customization continues...
I think I finally have something that will work for every page. Basically what I was looking for was the ability to customize various parts of my site based on which department's pages you were looking at. I didn't want to get a whole new master page for each department and I didn't want to have completely different ASPDNSF store instances.
What I've finally decided on is a hybrid of some of the things mentioned earlier on in this post.
web.config Mod
The first thing that I did was modify my web.config file.
I added this line to the <routeTable><routes> section:
Code:
<add name="SiteSection" url="{sitesection}/{pagename}" virtualPath="~/sitesection.aspx" checkPhysicalUrlAccess="false" />
This will take any link that looks like http://server/sitesection/page.aspx, and make a call to the sitesection.aspx page.
sitesection.aspx
The sitesection.aspx page is quite simple:
Code:
<%@ Page Title="" Language="VB" MasterPageFile="~/App_Templates/Skin_1/template.master" AutoEventWireup="false" CodeFile="sitesection.aspx.vb" Inherits="AspDotNetStorefront.SiteSection" %>
The CodeBehind page is also very simple:
Code:
Imports AspDotNetStorefrontCore
Namespace AspDotNetStorefront
Partial Public Class SiteSection
Inherits SkinBase
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Response.Cookies("SiteSection").Value = CommonLogic.QueryStringCanBeDangerousContent("sitesection")
Response.Redirect("~/" & CommonLogic.QueryStringCanBeDangerousContent("pagename"))
End Sub
End Class
End Namespace
This code simply sets a session cookie with whatever the text of the sitesection of http://server/sitesection/page.aspx is. It then redirects to http://server/page.aspx which will allow the normal routing rules to do their work.
Modifying Master.Template
Now that we have a session cookie that we can refer to we can modify the master.template file to act based on the contents of that cookie.
There are two ways that we can act based on the cookie. The first is to have some in-line code blocks. The following example will show how to insert a code block that will act based on the value of the cookie.
Code:
<%
If Request.Cookies("sitesection") IsNot Nothing AndAlso Request.Cookies("sitesection").Value.ToLowerInvariant = "altsite" Then
%>
[some HTML to appear on the page]
<%
End If
%>
A second way to include content on the master.template based on the value of the cookies is:
Code:
<%
Response.Write(CookieContentFunction())
%>
The above requires a modification to the CodeBehind file.
Modifying Master.Template CodeBehind
The modification to the CodeBehind file can be as complex as you want it to be; the only thing is that it should return a String that represents valid HTML.
With the above example the CookieContentFunction might look like this (added to the App_Code/MasterPageBase.vb file):
Code:
Private Function CookieContentFunction() As String
If Request.Cookies("sitesection") IsNot Nothing Then
If Request.Cookies("sitesection").Value.ToLowerInvariant = "altsite" Then
Return "<b>Your looking at the AltSite Mod.</b>"
End If
End IF
Return Nothing
End Function
Something even cooler would be modifying the menu after it is already built.
Modify Menu
In the App_Code/MasterPageBase.vb file a line can be added to the OnPreRender Sub directly after the call to SetupMenu. I named the new Sub SetupCustomMenu.
Code:
Private Sub SetupCustomMenu()
If aspnetMenu IsNot Nothing Then
If Request.Cookies("sitesection") IsNot Nothing Then
If Request.Cookies("sitesection").Value.ToLowerInvariant = "altsite" Then
Dim test As New MenuItem("Test Item", Nothing, Nothing, "~/default.aspx")
aspnetMenu.Items(0).ChildItems.Add(test)
End If
End If
End If
End Sub
This will add an item to the root of the menu that was automatically built by the page.menu.xml.config file. This method can be used to add, or delete, as many menu items as desired based on the value of that cookie. Thus, the menu can be changed with minimal effort by simply calling a link. This cookie can also be set any other way via code.
This allows menus, logos, and other elements on the page to be easily changed without having to maintain multiple master pages.