Pre-compile Web Page And Hide It From Preying Eyes

There are two main kinds of aspx files whichwe create in ASP.NET. The first one is created in VS.NET and it has thestatement similar to this one at the top of the page :

<%@ Page language="c#"Codebehind="ShowDetails.aspx.cs" AutoEventWireup="false"Inherits="ABSATree.ShowDetails" EnableSessionState="False"enableViewState="False"%>

List of attributes could be much longer thanthis. There are about thirty possible attributes and the explanationwhat they do is in MSDN. The other kind of aspx is usually created inordinary text editor or free environment like ASP.NET Web Matrix. Inthat kind of aspx file we usually insert our code in side scriptelement and Page directive is much shorter. Usually it looks like this:

<%@ Page Language="CS" %>

If we want to separate code from aspx file andavoid frequent recompilation of code we may do so. That could beachieved with help of Inherits attribute. Inherits contains name of theclass which should be inside some assembly located in bin directory andmust inherit from System.Web.UI.Page. How do we inherit from Page? Wemay try like this :

using System;
using System.Web.UI;
public class MyPage : Page
{
public MyPage()
{
this.Load += new System.EventHandler(this.Page_Load);
}
private void Page_Load(object sender, System.EventArgs e)
{
Response.Write("Hello World!");
}
}

We will save it as MyPage.cs and compile fromcommand line using csc /t:library MyPage.cs /debug. Assembly must endup in bin directory. Our aspx file will be extremely simple, it willcontain only this :

<%@ Page Inherits="MyPage" %>

When we point the browser to that page we willsee "Hello World!Hello World!". Not really what we want to see,Page_Load was called twice. Is that a bug? Not really, using oneadditional attribute fixes the problem. So aspx file becomes :

<%@ Page Inherits="MyPage" AutoEventWireup="false"%>

Now it is loaded only once. We may also addfew controls and see how it works, but this time we will doInitializeComponent like VS.NET instead of doing initializationmanually. There is no need to reinvent the wheel. This is aspx file :

<%@ Page Inherits="MyPage" AutoEventWireup="false"%>
<HTML>
<HEAD>
<title>MyForm</title>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<TABLE id="Table" cellSpacing="1" cellPadding="1" width="100%" border="0">
<TR>
<TD><asp:TextBox id="T" runat="server" /></TD>
</TR>
<TR>
<TD><asp:Button id="B" runat="server" Text="Done" /></TD>
</TR>
<TR>
<TD><asp:Label id="L" runat="server" /></TD>
</TR>
</TABLE>
</form>
</body>
</HTML>

Code behind looks like this :

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
public class MyPage : Page
{
protected TextBox T;
protected Button B;
protected Label L;
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.B.Click += new System.EventHandler(MyClickHandler);
}
private void MyClickHandler(object sender, System.EventArgs e)
{
L.Text = "Hello " + T.Text;
}
}

When we point the browser to it everythingwill work fine.There are other good sides of such approach beside hiding code fromcurious people. We are avoiding recompiling and if we don't have VS.NETwe can use .NET SDK debugger DbgCLR.exe to debug web page written insuch a way.

About Myself
I'm writing code and articles. I have about sixty published articles ondifferent web sites, most of it is on www.CodeNotes.com and currentlyI'm writing for them. I'm also preparing presentation for Web ServicesConference & Exposition in Toronto(http://www.wowgao.com/web_services_conference/index.php?Region=Global).If you have suggestions, questions or lucrative job offers my e-mail isfilipbulovic@hotmail.com.

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!