IIS vs onMissingTemplate

Several weeks back I was exposed to Python and Django for the first time, and it really got me thinking. While I'm not a huge fan of Python syntax, I really did like the setup for Django, and how it implements MVC. One of the first things that I loved was this little tid-bit for linking up URL requests to views:

from django.conf.urls.defaults import *

urlpatterns = patterns('',
(r'^polls/$', 'mysite.polls.views.index'),
(r'^polls/(?P<poll_id>\d+)/$', 'mysite.polls.views.detail'),
(r'^polls/(?P<poll_id>\d+)/results/$', 'mysite.polls.views.results'),
(r'^polls/(?P<poll_id>\d+)/vote/$', 'mysite.polls.views.vote'),
)

In that block, Django is defining regular expressions that map to Python functions, and defining how to pull named variables out of that regular expression, so that if you requested /polls/23/ becomes a call to the mysite.polls.views module to do the following:

# mysite.polls.views is the module, details is the function
detail(request=<HttpRequest object>, poll_id='23')

Isn't that cool? I'd love to be able to do that in ColdFusion, but it looks like there are a number of hurtles I have to get past before I can make this work:

  1. How do I make ColdFusion get the variables out of that URL path?
  2. How do I make ColdFusion look at a URL that doesn't exist?

Regular Expression Matching

First off, let me point out that CF does not natively support the (?P\d+) syntax that Django uses to capture variables, but I really do think that using some kind of hacker and CF8's REMatch() function, I can figure something out, and if not, I can appeal to the underlying Java classes, as described on Ben Nadel's blog. With a firm confidence that I can solve this problem when I get there, its now time to look at number 2.

URL Mapping

I'm running ColdFusion 8.0.1 on IIS 5.1 for the purposes of this testing.

Look at ColdFusion 8, there is one really nice bit of functionality that instantly jumped out at me as a very nice way to handle faux paths on a site, and that is onMissingTemplate() in an application.cfc. Theoretically, if you request a .cfm file that doesn't exist, your application is going to capture it and onMissingTemplate() will let you handle it. In actuality, its usefulness is very limited.

The onMissingTemplate() function has two big limitations, or should I say that IIS has big limitations. If you have a folder with nothing but your application.cfc, onMissingTemplate() will capture requests for any .cfm in that directory, no problem, but what if you aren't requesting a specific .cfm page (not specifying a file name), or what if you are trying to browse to a directory that doesn't exist?

In both cases IIS steps in before onMissingTemplate() can do a thing. Even if you set your default document in IIS to test.cfm, if it doesn't exist, IIS wont go there. Even though ColdFusion can handle requests for files that don't exist, IIS wont ask for your default document unless it exists. This isn't so bad really, you could just have a blank index.cfm, point IIS to it by default, and then have onRequest() and onMissingTemplate() call the same function to process them, but the next issue is the problem.

IIS wont process a request for a folder that doesn't exist. This means that you might want to use /blog/23/ as a URL, but unless the folder 23 exists in the folder blog AND you have an index.cfm in each of them, IIS will throw an error before it can get to ColdFusion.

Solution?

Well, I could look for some kind of URL rewriter for IIS, but that solution will only work if you control your IIS completely, so I've ruled it out. The only other idea I have left completely depends on if long, complicated file names are any better then URL variables... I mean is "http://www.jonhartmann.com/blog.23.cfm" any better then "http://www.jonhartmann.com/index.cfm?page=blog&id=23"?

 
Comments are not allowed for this entry.
Jon Hartmann, July 2011

I'm Jon Hartmann and I'm a Javascript fanatic, UX/UI evangelist and former ColdFusion master. I blog about mysterious error messages, user interface design questions, and all things baffling and irksome about programming for the web.

Learn more about me on LinkedIn.