One of the things I do for me treasure is create custom web applications for SMBs, and often the client already has some infrastructure in place. Often times PHP is available, but sometimes they just have a basic hosting account on a windows box somewhere so the only language available is ASP. I’ve actually had a few gigs lately that needed somewhat complex sites, and they needed to be done is ASP.

I looked around for a framework for ASP. Something like Fusebox. A nice, easy model / view / controller style framework for a bit of modularity, and I couldn’t find anything. Since I will probably be getting more gigs like this, and I hate writing the same code twice, I decided to write my own simple framework. I have no idea if anyone who regularly uses ASP cares about this kind of stuff, but I figured I’d put it out there so people can at least have a base if they want (and at the minimum to document the framework for myself).

If you’ve used Mach-II, Struts, Fusebox, or RoR nothing in the framework will shock or amaze you. It’s designed to be simple and easy with little configuration. Here is how it works.

Concept

The basic concept is you create model objects in the Model directory, you create view page(s) in the View directory, and you link them together with a controller page in the, you guessed it, Controller directory. There is no need to create a file anywhere except one of these 3 places, and there is no XML type control file.

A URL variable is passed into the framework that specifies the Controller and the Method to be called. The Controller page (which is not an object) does some work, creates any model objects it needs, and writes it’s output to user defined variables in a Global Scope (more on this in a minute).

For a quick example, the URL http://a.com/index.asp?C=Books.ShowBookForm would call the ShowBookForm method defined in the Books.asp controller. The Books controller can then create any objects and do anything it needs. It can then set it’s output to variables in Session(…) for the view page to display. Finally, it should set Session(”VIEW”) to say what main view page should be used for display.

View

Now all the controller output is being written to the Session scope, but the framework hijacks the Session scope and turns it into a Request scope. At the end of the request the Session scope is destroyed, so the life cycle of the Session scope only lasts for the duration of the request. I had to do this because it seems the Request scope in ASP is readonly, and the Session scope is the only scope that persists through sub processes. (For session tracking I tend to use a database and cookies to manage the sessions anyway (I’ve found it helps with scalability)).

So on the view page, you can display any controller output by doing things like:

<h1>Hello <%= Session("out.loggedInGuy") %></h1>

Where “out.loggedInGuy” was set by the controller. You can also conditionally include other views (that can show Session(…) variables as well). For example, the controller could set a variable to specify which include should be shown in the “main content” area of the page using Utils.ShowView(view). Like so:

<!-- #include file="../AppCore/Utils.asp" -->

...

<div class="mainToolbar"><%= Utils.ShowView("ToolBar.asp") %></div>
<div><%= Utils.ShowView(Session("out.content")) %></div>
<div class="mainRightSide"><%= Utils.ShowView("RightBar.asp") %></div>

...

Controller

The Model, in general, is a single class per file that does something, and the View is a typical ASP page with the Session(…) items put where you want the output to be displayed. The controller is an ASP page that ties them together. I couldn’t figure out how to make them objects and work properly in the sub process (see “Bit More Information”), so I opted to define controller pages as swtich pages (a la old school Fusebox). An example controller page looks like the following (assume it’s named LoginController.asp):

<!-- #include file="../Model/Login.asp" -->
<%
	Select Case Session("METHOD")
		Case "DoLogin"
			Dim MyLogin
			Set MyLogin = new Login
			MyLogin.DataFile = Session("SERVER_INSTALL_PATH") & "AppDatastorepasswords.dat"

			UserLevel = MyLogin.DoLogin(Request.Form("username"), Request.Form("password"))

			If(UserLevel = "ADMIN" Or UserLevel = "USER") Then
				Session("out.content") = "Forms/Welcome.asp"
			Else
				Session("login.error") = "The username or password passed was wrong!"
				Session("out.content") = "Forms/Login.asp"
			End If

		Case "ShowLogin"
			Session("out.content") = "Forms/Login.asp"

	End Select

	Session("VIEW") = "Main"
%>

Pretty straight forward. It looks at the passed method, if it’s the DoLogin method it creates a Login object from the Model directory, and tries to login. If it passes, it sets the pages user defined “main area” to a welcome screen, and if it fails, it sets it to the login form (and sets some display able text defined on the login form).

A URL to use the above controller would look like: http://a.com/index.asp?C=LoginController.ShowLogin, and then the form would post to http://a.com/index.asp?C=LoginController.DoLogin.

Speaking of form posts, one more Utils method is Utils.CreateLink(strController, strMethod). This does a bit of work for you when creating links or form postings by making a link to the proper controller / method. For example, our login form that is processed above could look like this:

<!-- #include file="../AppCore/Utils.asp" --> 

<div class='error'>
<%= Session("login.error") %>
</div>

<form name="login" action="<%= Utils.CreateLink("LoginController","DoLogin") %>" method="post">
	User Name: <input type="text" name="username">
	Password: <input type="password" name="password">
	<input type="Submit" name="submit" value="Login">
</form>

This keeps the form posting link from being hard coded, and allows the form to be reused in other applications. You could also set Session(…) variables for the controller and method parts of the CreateLink method if you were so inclined.

Main Parts

The main parts of the framework. AppCore/Settings.asp has stuff you may want to / need to change. Any new install should review that file and make sure it looks ok.

Here is a quick reference to the main parts of the framework:

Session("CONTROLLER") - the controller portion from the url (information only)
Session("METHOD") - the method portion from the url (information only)
Session("VIEW") - the main view page to use for the request (controllers should set this)
Session("APP_DEBUG") - if the application is in debug mode (information only)
Session("INSTALL_PATH") - the path to the root of the application from the browsers point of view (information only)
Session("SERVER_INSTALL_PATH") - the path to the root of the application from the server file system point of view  (information only)
Session(strUserDefined) - any variables that the controller wants / should pass to the view

<!-- #include file="AppCore/Utils.asp" --> - to get the Utils object
Utils.AddError(strErrorText) - add a processing error (for display on the error page)
Utils.ShowView(strViewPage) - a page in the View directory
Utils.CreateLink(strController, strMethod) - create a URL to the passed controller and method

Bit More Information

ASP is kind of unusual with includes. You can only include them with <!–#include, and the file is included *before* it is compiled. That means there is no way to conditionally include a file into the current request. The only thing I found that comes close to a runtime include is the Server.Execute() method. However, this creates a sub process which makes it hard to / not possible to communicate with the parent process which is why I hijacked the Session scope.

Download / License

I am using this framework for non-enterprise, but in need of modularity, ASP applications for SMB. This is not a project per se – it is just something I am using, and I thought others might benefit from it.

If you’d like to use it, you can download it and use it under the terms of the GPL.This framework is in the Public Domain. You can download the files here. As for installation, you should be able to just unzip them on your server and it should work out of the box.

Oh and it’s called Mv Casp – for MVC ASP. Not very original I know.

Comments

This entry was posted on Tuesday, September 19th, 2006 at 10:26 am and is filed under General, Web Apps, Windows. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

13 Comments so far

  1. Behess on March 4, 2007 6:27 am

    Hello ! I ‘ve read your article and the only point I deplore is the licence which is too restrictive for a professionnal usage. You should pass it ont LGPL, and so I would be happy to use your framework a lot !!!

  2. [...] a few small ones a while ago. To get the applications out faster, and reuse common bits, I wrote a simple MVC style framework (I wrote one because I couldn’t find an existing one for [...]

  3. Behess on March 14, 2007 1:24 am

    How cool you are, thanks a lot !!

  4. Sunaryo Hadi on March 14, 2007 8:58 pm

    This ideas are very interesting. It should popular! I love Fusebox. Anyway, there’s other popular framework for PHP, cakePHP. Still not learn yet cakePHP, but it should be also interesting

    Btw, I have try your mvcasp. Still not work yet!

    Microsoft VBScript runtime error ‘800a01ad’
    ActiveX component can’t create object: ‘Scripting.FileSystemObject’
    /mvcasp/AppCore/Utils.asp, line 39

    Btw, any documentation yet on this? I would like to join the team, to develop more in the future.

  5. Steve on March 15, 2007 4:43 am

    It would be easier to create a fusebox 4 engine then a fusebox 2 version. In Fusebox 4 your controller is a single, or set of, xml files that depict your logic. The core file transverse these xml documents and creates a separate parsed file that includes all the files depicted in your fuse. This parsed file is what is included in CF but would be best executed with a Server.Execute in ASP.

    I have been debating on creating ASPFusebox for a while since the client I’m supporting is still using classic ASP, but the client has opted for a migration to ASP.NET instead.

  6. pav on May 22, 2007 3:51 am

    Hi Guys,

    Please check this out. ASP MVC framework!

    http://www.troika-asp.com

    It uses OO JavaScript for Model and Controller logic and XSLT transformation templates for the View.

    Pav.

  7. Dee on August 16, 2007 6:35 am

    I like this approach, simple :).

    I think it is more practical than Troika.asp, which is very complex and verbose, even for small screen displays.

    I wonder if the dictionary object could have been used instead of the session object.

    I am also curious about any specific problems encountered for use of Server.execute for dynamic includes .

    thanks for sharing !

  8. Christian Calderon on August 31, 2007 6:23 am

    I wrote a framework very simular to ASP.NET some time ago. It is very popular and widely used now. Check it out at
    http://www.claspdev.com

    Regards,

  9. Fabio Zendhi Nagao on January 29, 2008 11:37 am

    Hi, thank you for this work. I’ve actually build a new ASP Framework based in this one. Take a look in the site: http://zend.lojcomm.com.br/goodies/asp-xtreme-evolution/ to find an evolved version of this concept.

  10. Andre Vinics on March 6, 2008 5:10 pm

    Hi,
    I have created a data access object to get some data and I’m trying to send then to View layer, but the asp Dictionary object dont survive to session.execute. Please, Can you write a more complete sample with database iteration and maybe a DAO like layer into?

    thank you

  11. Gani on September 23, 2008 3:18 am

    I’d Try your framework with your own code, in this page…
    So I create folder “Forms” in folder “View” and create file login.asp

    When its running…
    Utils.ShowView(Session(”out.content”)) cause an error…

    So code include file in login.asp, is wrong…
    before…

    have to be …

    It Work..

    I think I will use your framework for my project..

    Cheers :)

  12. a on November 11, 2008 3:51 pm

    ^^

  13. [...] Pessoal estou desenvolvendo uma framework asp MVC. Ela pe baseada no projeto MVCASP. http://robrohan.com/2006/09/19/simple-mvc-asp-framework/ [...]

Name (required)
Email (required)
Website
Share your wisdom