Web Project Conversion - IConvertedPage
As I've mentioned previously, I'm currently in the midst of a project to convert an old web application from .net 1.1 to .net 2.0. This is a moderately sizable application containing 40+ aspx pages. We are not simply converting the project and code-base from .net 1.1 to .net 2.0 - but we are also incorporating new features from .net 2.0 into our old project. Some of the features included are:
Since the theming property in the page is being set by a user profile - I included that call within the Preinit event in the custom Page Base - so we would not need to include code on EVERY page to set that property.
But then again, since the code is in the Page Base class and every page uses that Page Base (it inherits from System.Web.UI.Page), some pages were throwing an exception when that property was being set. The reason for the exception was because .net 2.0 requires pages that are using theming to include the "runat="server"" attribute in the <head> tags of each page. If it does not contain that tag - .net 2.0 throws an exception.
So, I had a conundrum - how would the Page Base know if a webpage was actually converted from .net 1.1 to .net 2.0? My solution was rather simple, actually. I created an interface called "IConvertedPage" with a simple read-only property called "IsConverted":
Public Interface IConvertedPage
ReadOnly Property IsConverted() As Boolean
End Interface
The developer then adds the "Implements IConvertedPage" statement to the code-behind for their page when they have completed converting the page from the .net 1.1 structure to .net 2.0, as so:
Partial Public Class search
Inherits PageBase
Implements IConvertedPage
Then, in the Preinit method of the PageBase, you can easily check 2 things:
- The Page implements the IConvertedPage interface.
- The value returned from the IsConverted() property is true.
This is shown as the following (the code is consolidated into one location, but in actuality, there is a private property I wrote to check whether the page has been converted):
Private Sub Page_PreInit(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.PreInit
If Me.Principle IsNot Nothing _
AndAlso TypeOf (Me) Is Components.IConvertedPage Then
'// Only apply the theme and the masterpage when
'// the page has been fully converted, as evidenced
'// by the implented interface.
Dim iConvPage As Components.IConvertedPage = _
CType(Me, Components.IConvertedPage)
If iConvPage.IsConverted Then
Me.Theme = Me.Principle.UserInfo.ThemeName
End If
End If
End Sub
This method also has the side-effect of providing a nice way of determining in the PageBase whether advanced .net 2.0 functionality should be applied to a particular page.
Very useful - are there any other ways that people have combated this issue?