22
Compile your asp.net mvc Razor views into a seperate dll
Posted in .net, asp.net mvc, c# by Chris van de Steeg
Inspired by David Ebbo’s blog post ‘Turn your Razor helpers into reusable libraries’ I wanted to be able to embed compiled Razor views in a dll. This would allow for easy distribution of asp.net mvc ‘modules’ that have their default views embedded, but allowing you to place files in your ‘views’ folder to override those default views.
To generate the c# code for the views, I just started out with David Ebbo’s single file generator and modified it to generate views instead of helpers only. To do this, there had to be a WebRazorHostFactory that knew about all the mvc stuff (what dll’s to reference, what namespaces to use, etc) .I could either choose to include all that information statically in the code, or I could look for a web.config in the same project and let the WebRazorHostFactory use that config. I chose the latter option, so that everyone can easily change the WebRazorHostFactory behavior by adding a web.config file to their project.
Next, I started figuring out how the PageVirtualPathAttribute inside System.Web.Webpages.dll is used by Microsoft. I found out that it is used when you call ApplicationPart.Register and after that call, there’s not much you can use of this functionality without some heavy reflection. I know it’s the wrong path to choose, but did it anyway. I ended up creating a ViewEngine calling into ApplicationPart’s internal methods to output compiled views. It worked, but I didn’t like it much: there had to be a better way.
I then tried if I could hook into the BuildManager stuff that asp.net mvc uses to generate the views. It was actually much easier then expected, now why didn’t I go on that path the first time!
I ended up creating
- a CompiledRazorBuildProvider, which inherits from the default RazorBuildProvider,
- a CompiledVirtualPathProvider which returns a CompiledVirtualFile if it decides that a compiled view should be used
- and a ApplicationPartRegistry, which registers which compiled views are available.
To view the source, head over to
- https://github.com/csteeg/BoC/tree/master/Src/Commons.Web.PrecompiledViews/ – for the classes mentioned
- https://github.com/csteeg/BoC/tree/master/Src/RazorSingleFileGenerator/ – for David Ebbo’s modfied file generator
To just get started without viewing the source, read on, I will explain step by step how to use this library with some screenshots.
If you have improvements, fork the project on GitHub and let me know! If you have suggestions for improvement, just drop it in the comments.
Step 1: Install the FileGenerator
Download and install the Visual Studio extension: http://visualstudiogallery.msdn.microsoft.com/en-us/f28290ce-d987-4f91-b034-707031e10ce6/file/39295/0/RazorSingleFileGenerator.vsix
Step 2: Create new mvc project
Step 3: Add a class library to hold the views
Step 4: Cut your Models & Views folders
Step 5: Paste them into the just created Class Library
Step 6: Copy the website’s web.config file
Step 7: Paste that web.config file into the class library
Step 8: Select all your .cshtml files in the class library and set the Custom Tool to ‘MvcRazorClassGenerator’
Does anyone have a suggestion on improving this step??
Step 9: Build your class library
Step 10: Add your class library to the references of the web application
Step 11: Add ‘Commons.web.mvc.precompiledviews.dll’ as a reference to your website
this dll is copied to the class library’s output folder (in this case ../EmbeddedViews/bin/debug’)
Step 12: Register your views by adding a line to the application_start in global.asax.cs
Step 13: Run your website!
Now, you will see the normal default website, even though there aren’t any views in your website path!
If you wish to override some views, just create the normal folders (eg /Views/Home) and add your views there, but don’t forget to copy back the deleted /Views/web.config back into your project then!
05
HappyBirthday app – Vodafone App Star Country winner
Posted in vf360 by Chris van de Steeg Tags : mobile, vf360, widget
I’m very (very!) happy to inform you all that my mobile app HappyBirthday! has won the 1st price at the Vodafone AppStar competition for the Netherlands. This also means, the app is now 1 of the eight apps in the the grand final! The first price of the finals is another € 75.000 !!
So I kindly ask you to all go vote at the Vodafone App Star website and get me that first price!
HappyBirthday is an app for the Vodafone 360 platform and helps you to never forget a birthday again.
You can read more about the app at the details page of HappyBirthday!
The Vodafone 360 platform is a new internet service for your mobile, PC and Mac. It brings your phone, email, chat and social network contacts together in one placeand runs on many many phones. You don’t need to have a vodafone account to join 360, anyone can join at http://360.com.
13
ASP.NET MVC, DynamicData, Domain-/RiaServices, Unity and NHibernate: Part 1
Posted in asp.net mvc, nhiberntate, RiaServices, unity by Chris van de Steeg
I’ve been playing with RIAServices and DynamicData lately, and managed to combine these with NHibernate as ORM in an ASP.NET MVC project.
For this project the following (extensive) list of libraries is used (all these libraries are included in the download):
- MS ASP.NET Mvc 1.0
- Microsoft .NET RIA Services July 2009 Preview
- NHibernate 2.1.0CR1
- NHibernate Linq (latest trunk version)
- FluentNhibernate (latest trunk version, patched to work with NHibernate 2.1.0CR1)
- MS Unity 1.2
- ASP.NET Dynamic Data MVC Preview (since this project is pretty old, I had to modify it quite a bit, the source is included in the download)
- I used Rob Conery’s t4 templates as the base to generate entities from the database.
Besides using these libraries, I copied some code snippits from the Kigg project, which has quite a nice codebase!
In the first part of this serie, I’ll show you how you can use these libraries along with my own code to scaffold the entire Northwind database in an mvc project. It’ll be totally hassle-free, I promise! It will generate classes from the database-schema and I will also show you how to override the default generated domainservices and repositories (yes, those are generated at runtime if you don’t specify your own!).
In the upcoming parts, I will add client-side validation and some ajax-love to the scaffold pages. In one of those parts I’ll also explain how my code works, and what it does. I’m also thinking about showing how to replace some parts of the library, since it’s very pluggable. I could for example replace the persistencelayer with Linq-to-Sql or replace the Depency part with StructureMap.
Although this sample shows you how to use an existing database, it would ofcourse be best if you could use the Model-first approach! Luckely nHibernate makes this very simple as well. If you create the entity files in your project and have them inherited from BaseEntity, all you have to do is add this line to your Application_Start:
new SchemaUpdate(IoC.Resolve<Configuration>).Execute(true, true);
Your database schema will be automatically updated if you change anything to your Model.
You can watch the screencast here and download the sample + dll’s here
For the next screencast, I’ll by a mic so I don’t have to subtitle it
