On my current project, I converted hundreds aspx pages from ASP.NET 1.1 to 3.5 using SED. SED was new to me so there was some learning curve and nuisances to overcome, regular expressions to fine tune, etc. However, in my opinion, it was worth the effort to create an automated and repeatable conversion process below is an example sed conversion script.
|
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Conversion._Default" %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
< html xmlns="http://www.w3.org/1999/xhtml" >
< head runat="server">
<title>Untitled Page< FONT>title>
< FONT>>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="userNameLabel" runat="server" Text="User Name">< FONT>asp:Label>
<asp:TextBox ID="userNameTextBox" runat="server">< FONT>asp:TextBox>
<br />
<asp:Label ID="passwordLabel" runat="server" Text="Password">< FONT>asp:Label>
<asp:TextBox ID="passwordTextBox" runat="server">< FONT>asp:TextBox>< FONT>div>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
< FONT>>
< FONT>>
< FONT>> |
This is an example of an ASP.NET 1.1 page that has doctype, head, body, and form tags. When converting to ASP.NET 2.0 or better, these pages are typically replaced with a master page implementation. Thererfore, these elements have to be removed from every 1.1 page. You could use your favorite search and replace tool; however, every tool that I tried including (VS.NET, Advance Search and Replace, and Actual Search and Replace) does not have the ability to delete the line it finds. So, if you were to use these tools in an attempt to do your conversion, then you end up with source code with a bunch of blank lines. I did not think this was acceptable and turned to sed which allows me to delete the line when it finds a match.
Desired End Result
|
<%@ Page Language="C#" MasterPageFile="~/DefaultMaster.Master" AutoEventWireup="true" Codebehind="DefaultAfterConversion.aspx.cs" Inherits="Conversion.DefaultAfterConversion" Title="Untitled Page" %>
< asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<div>
<asp:Label ID="userNameLabel" runat="server" Text="User Name">< FONT>asp:Label>
<asp:TextBox ID="userNameTextBox" runat="server">< FONT>asp:TextBox>
<br />
<asp:Label ID="passwordLabel" runat="server" Text="Password">< FONT>asp:Label>
<asp:TextBox ID="passwordTextBox" runat="server">< FONT>asp:TextBox>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" />
< FONT>div>
< FONT>asp:Content> |
SED Script
Below is an example of the sed script I used to convert my pages. You can copy this text and save as c:\temp\ConvertToMaster.sed.
|
# process the page eleement s/<%@ Page [Ll]anguage="c#"/<%@ Page Language="c#" MasterPageFile="~\/DefaultMaster.master" Title="PageTitlePlaceHolder"/
#replace title #s/^D\(.*\)/\1\tOUTY\tINNY\t/ #s/<[Tt][Ii][Tt][Ll][Ee]>\(.*\)<\/[Tt][Ii][Tt][Ll][Ee]>/\1/
# remove doctype /
# replace html with content placeholder s/<[Hh][Tt][Mm][Ll]>//
# remove opening head tag /<[Hh][Ee][Aa][Dd]/d
# remove closing head tag /<\/[Hh][Ee][Aa][Dd]>/d
# remove opening meta tags /<[Mm][Ee][Tt][Aa] /d
# remove closing meta tags /<\/[Mm][Ee][Tt][Aa]/d
# remove link /\<[Ll][Ii][Nn][Kk] /d
# remove opening body tag /\<[Bb][Oo][Dd][Yy] /d
# remove closing body tag /<\/[Bb][Oo][Dd][Yy]>/d
# remove opening form tag /\<[Ff][Oo][Rr][Mm] /d
# remove closing form tag /<\/[Ff][Oo][Rr][Mm]>/d
# remove head closing tag /\<\/[Hh][Ee][Aa][Dd]>/d
# replace html closing tag with s/<\/[Hh][Tt][Mm][Ll]>/<\/asp:Content>/
# remove register tags for component art /<%@ Register TagPrefix="componentart"/d
# remove webchart register tag /Assembly="ComponentArt.Charting.WebChart"/d |
In this example, the comments provide context on what the intended action should be for each sed command. Now, to perform this transformation on all aspx pages in your application, you can execute a statement like the following from an command window.
for /R "" %f in (*.aspx) do ssed.exe -i*.bak -f c:\temp\ConvertToMaster.sed "%f"
On subsequent executions of the above command, you need to delete the backup files. A batch file with the following contents does the trick nicely.
pushd "YourSolutionDirectoryNameHere"
del *.bak /s
popd
Nuisances
- enabling delayed variable expansion did not allow me to create a batch file containing multiple sed statements. As a comprimise, I just saved each for loop in a text file and copy and pasted into a command window to execute.
- You have to understand regular expressions at some level to use sed.
- sed can aggressively delete lines. In some instances, I found that my sed script was too aggressive and had to readd some lines. This was relatively easy to do by comparing folders of ASPX files using Beyond Compare.