Stefan Says

Opinions on ASP.NET, ASP, databases etc
posts - 32, comments - 25, trackbacks - 23

Wednesday, June 20, 2007

Adding controls and pages to a CMS

I have talked earlier on codebedind vs inline code and this topic touches that area.

The problem:
Client is running a website, using a CMS they don't have the source for, but rather a binary license. Now they wanted to integrate
a) a control reading from database and showing some stuff
and
b) a 404 handler (preferably ASPX). It should update a database

If you know me you know about me being a "need of control" freak. I'll take my own code over 3rd party binary components    any day a week, if possible. So, I felt a little lost. Then it struck me, it's so easy actually.

For a)

Create the control - nothing special about it. Lets say it's called RefController.
Then we can add the dll to the bin directory - and it to the webpage
<%@ Register TagPrefix="RefCtl" Namespace="RefController12" Assembly="RefController" %>

bla bla html stuff
<RefCtl:RefControl id="hhh2222"  runat="server" The Property="thevalue"></RefCtl:RefControl>
bla bla html stuff

And next time the page is accessed our control will be used like anyother control on the page. 
Even though we havn't recompiled the main web application.

As for b)
This is really cool if you ask me. Cause you can of course also add ASPX pages to a website without needing to
recompile the webapp.


I created a file 404handler.aspx and out all source as inline:


<%@ Page language="c#"   %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>404Handler</title>
  <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
  <meta name="CODE_LANGUAGE" Content="C#">
  <meta name="vs_defaultClientScript" content="JavaScript">
  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
  <script language="C#" runat="server">
  protected void Page_Load(object sender, System.EventArgs e)
  {
   string DBConn = "Server=dbconn";
   string sPage = Request.QueryString["aspxerrorpath"];
   if ( sPage == "favicon" )
    return;
    
   string sExt = System.IO.Path.GetExtension(sPage).Replace(".", "").ToLower();
   if ( sExt.Length == 0 )
    return;
   if ( sExt != "aspx" && sExt != "php" && sExt != "asp" && sExt != "html" && sExt != "html" )
    return;
   if( sPage != "" )
   {
    //Add to database
   }
   else
   {
   }

   sPage = "http://www.wwwwwwwwww.com/hello.aspx"
   Response.Redirect(sPage, true );
   return;
   

  }


Pay NO attention to the actual code, what I doing on Page_Load is of no matter.
The cool thing is that we, by this technique, are able to create independent components and
pages and just throw them into a site whenever needed. Regardless of what CMS (well, it rather be
ASP.NET based of course).

posted @ Wednesday, June 20, 2007 1:37 PM | Feedback (0)