Scripting Asp.net MVC Controllers at Runtime
Experimenting with new features can really be time consuming in a statically compiled web-framework like Asp.net. Although it is possible to edit view-files at runtime, every time you need a new method in your controller or get that last LINQ-Query right, you have to …
…. (1) Restart your Web application
…. wait …
… wait even longer if the project is big …
… navigate to the the page where you are working at …
… just to find out that the behaviour is still not what you have expected …
… start from (1)
Well that can suck sometimes and costs you a lot of your spare time.
But what if you could …
- add controllers or controller actions …
- change / add linq queries …
- add new types …
- just do what you want with your controller files ..
… at runtime?
What if you could see your changes immediately after a F5 Browser refresh?
Well, this is possible now :)
The latest Mono.Csharp compiler update adds support for evaluating full classes at runtime, which opens a whole new world of possibilities.
When I read Miguels post about the new compiler as a service update, I came up with the idea of using this awesome library to allow changing mvc controller code at runtime – like you already can do with the markup in *.aspx or *cshtml files.
First of all, lets see the MVC Runtime Controller Factory in action:
What is RuntimeControllerFactory?
An Asp.net MVC 2 ControllerFactory including some classes which allows you to edit your Controller classes at runtime, without restarting your application. You can add controllers, actions and classes during your web-app life cycle and just need to refresh your browser. This can be very useful when you are prototyping new features.
Where is the code?
All the code of this working prototype is in my launchpad code repository.
Update: All code is availble on github now: https://github.com/davrot/RuntimeControllerFactory
All code is MIT/X11 licensed. Feel free to change the source so it fits for your needs.
How does it work?
- MVC executes IController GetControllerInstance (RequestContext requestContext, Type controllerType)
- RuntimeControllerFactory creates a new Mono.CSharp Compiler Service
- Depending on the requestContext, the path prrovider looks for the correct %Controller file in your web project
- The content from the Controller.cs file is passed to the evaluator (Namespace declarations are stripped out because they are not allowed in the evaluator)
- The newly created controller from the evaluator is returned
How to include this into my project?
- Using the pre-compiled dll:
- Reference the assembly Fusonic.Web.Mvc.RuntimeController.dll
- Add the following code in your global.asax.cs file’s Appliaction_Start()
protected void Application_Start() { RuntimeControllerFactory factory = new RuntimeControllerFactory(new DefaultPathProvider()); factory.ReferenceAssembly(Assembly.GetExecutingAssembly()); // ... reference additional assemblies here ... ControllerBuilder.Current.SetControllerFactory(factory); }
- Using the source
- Download the source from the bazaar repository
- Experiment with the test application
- Reference the RuntimeControllerFactory project in your solution …
Is this useful for me?
RuntimeControllerFactory can increase your productivity when you are prototying new features in Asp.Net MVC and I hope it will be useful for lots of people out there.
Due to the limitation of only editing controller files, it will not be suitable for every developer/project. You know your daily business best, so you have to decide if this feature is for you – but it is a nice demonstration of the Mono Compiler as a service though ;)
David.
Tags: Asp.net mvc, mono, mvc
Amazing! I truly hope you can come to Monospace in Boston this July and give a talk about this. Email me.
Great… is it working with asp.net mvc3? thanks
Yes, the latest version works with mvc3. But keep in mind that mono does not ship MVC3 so you have to reference the binaries manually.
[...] http://blog.fusonic.net/2011/04/scripting-asp-net-mvc-controllers-at-runtime/ [...]