Editable Routes Using App_Code

UPDATE: I’ve updated the original post for editable routes to work in medium trust and not require a full app domain reload like this approach does. I think that approach may supersede this approach until I learn otherwise. :)

Yesterday I wrote about a technique using dynamic compilation to allow editing routes after you’ve deployed an application without having to manually recompile your application.

I made use of a FileSystemWatcher to monitor a Config directory and dynamically recompiled code when the code file changed. This has one advantage over using the App_Code directory in that the whole App Domain doesn’t need to get recycled when you make changes to your routes.

Today, my co-worker David Ebbo (who’s a master at ASP.NET Build and Compilation system) pointed out one gaping flaw with my approach. It doesn’t work in Medium Trust because the FileSystemWatcher class demands full trust. Doh!

For many business systems, that may not be a concern. But for my blog engine, it’s a huge concern. There are workarounds to the FileSystemWatcher issues, but I decided to take the easy way out and use the App_Code directory approach, since it handles all that crufty logic for watching the file system for me.

In this case, I simply added a new folder named App_Code to my project and copied Routes.cs to that folder.

routes-in-app_code

I then added a new method to RouteRegistrationExtensions. (Note that the actual code has some null reference checking which I omitted here.)

public static void RegisterAppCodeRoutes(this RouteCollection routes) {
  var type = BuildManager.GetType("Routes", false/*throwOnError*/);
  var registrar = Activator.CreateInstance(type) as IRouteRegistrar;
  registrar.RegisterRoutes(RouteTable.Routes);
}

So instead of the method I wrote in my previous post, I call this method. The nice thing here is that this method doesn’t have to worry about attaching a FileSystemWatcher or handling events and reloading routes.

Any time the Routes.cs file is changed, the entire App Domain is restarted and Application_Start is called again.

I want to also point out that a long while ago, I showed a different approach for editable routes using IronRuby that you might be interested in.

You can download the sample project here which includes both methods for doing editable routes.

Technorati Tags: ,,

What others have said

Requesting Gravatar... Pita.O Jan 18, 2010 12:02 PM
# re: Editable Routes In Medium Trust
Hi Phil,
This sounds great, especially for the use case you describe. One key problem is that the cloud is here! We no longer have those luxuries of change without compiling. We will always have to repackage even to change web.config. So if the route cannot be configured in .cscfg then it won't save you much of the roundtrip (cloudwise). Just for perspective.
Requesting Gravatar... ben Jan 18, 2010 4:17 PM
# re: Editable Routes In Medium Trust
I like it.
Requesting Gravatar... haacked Jan 18, 2010 4:54 PM
# re: Editable Routes Using App_Code
By the way, I updated the original post to work in medium trust and I think it's probably a better approach.
Requesting Gravatar... haacked Jan 18, 2010 5:00 PM
# re: Editable Routes Using App_Code
@Pita Are you saying that content files, virtual path providers, etc. won't ever work in the cloud?

Most likely, my IronRuby approach would still work because content files are allowed. haacked.com/...
Requesting Gravatar... John Jan 19, 2010 12:14 AM
# re: Editable Routes Using App_Code
As you are using the app_code I am correct in thinking for MVC projects that are created as Web Applicaions (rather than website projects) this will not work as you cannot use the app_code folder?
Requesting Gravatar... Aleč Roubíček Jan 19, 2010 1:16 AM
# re: Editable Routes Using App_Code
@John there is no reason to App_code should not work with WAP. They are same at runtime.
Requesting Gravatar... John Jan 19, 2010 6:36 AM
# re: Editable Routes Using App_Code
After some research App_Code is not supported in WAP ( msdn.microsoft.com/.../aa983464%28VS.80%29.aspx), you do not get the option to add it and if you add it manually it then publish the site compiles the files it into the main DLL and does not include the directory.

I guess the other method will be the best option in that case.

Thanks
Requesting Gravatar... Stephen Jan 19, 2010 9:56 AM
# re: Editable Routes Using App_Code
I remember writing something along the lines of the first series of this blog, The route file being watched was in a network drive though and everytime it went down, the filewatcher class would just failt to keep monitoring changes. In the end, i just felt that the extra coding and complexity introduced was not worth the benefit. Very good article.
Requesting Gravatar... haacked Jan 19, 2010 11:05 AM
# re: Editable Routes Using App_Code
@John Aleč is correct. Code in the App_Code directory is compiled at runtime using the ASP.NET compilation system.

The distinction between Web Application and Web Site projects is a Visual Studio Project distinction, not a runtime distinction.
Requesting Gravatar... Salman Farsi Jan 19, 2010 11:54 PM
# re: Editable Routes Using App_Code
Sounds greate
Requesting Gravatar... Thanigainathan Jan 20, 2010 6:09 AM
# re: Editable Routes Using App_Code
A fantastic article from Haacked.
Thanks,
Thani
Requesting Gravatar... bret(runxc1) Jan 20, 2010 2:02 PM
# re: Editable Routes Using App_Code
So does this mean that you are working on an edition of Subtext that will use ASP.NET MVC 2.0??

There are very few good open source asp.net mvc applications out there. I have been thinking of changing my blog provider or rolling my own.
Requesting Gravatar... iPhoneKönig Jan 24, 2010 3:39 AM
# re: Editable Routes Using App_Code
Thank you for these useful informations. i will apply them right away
Requesting Gravatar... Andrew Jan 25, 2010 3:05 AM
# re: Editable Routes Using App_Code
Not a bad solution.
Another solution would be to put your routes in an Xml file, load it on application_start and set up a cache dependency to check for changes. Less elegant sure but at least it doesn't restart your app, and I don't like the pattern of dropping .cs files into your app_data folder and automatically kicking off a recompile. Sounds like a potential configuration nightmare.

Thanks for the great article though.
Requesting Gravatar... John Dyer Feb 02, 2010 6:15 AM
# re: Editable Routes Using App_Code
I'm trying to use something like this to build a CMS (in 4.0) that uses a catch-all and constraints to determine which URLs to handle. The problem I'm getting is that catch-all/wildcards don't catch URLs with a trailing slash (/). I know it's a bit off topic, but do you know a way to catch the trailing slash on a catch-all?

What do you have to say?

(will show your gravatar)
Please add 1 and 7 and type the answer here: