Over the last few days, I have been working on a sample which shows how you can build your custom library of helpers using the Razor syntax and use NuGet to distribute this library as a package so that MVC or WebMatrix application developers can use this package of helpers. This is a great way of writing resuable helpers for most of the common tasks that developers would be doing and using NuGet distribution channel to reach out to all the developers.
To start off, you can write the Razor helpers in the Editor of your choice(VS / WebMatrix). You can then write a console application that parses all these helper files and generates a binary out of it. The following code snippet shows you how you can compile a number of helper files into a binary. This program takes in
At this point we have a binary which has all the helpers. In the next steps I will show you how to create a Nuget package for this RazorToMVCViaNuGet.dll and add it to the package feeds. Before I go any further you can get more information about installing Nuget and creating packages from Nuget Codeplex site. Make sure you download Nupack.exe along with downloading NuGet. This exe does not come with the Nuget download and has to be downloaded separately from the same place as Nuget
Now that you have NuGet and Nupack.exe, let’s create our package now.
1. Create a folder called Demohelperpackage
2. Create a .nuspec file based on the Nuspec format for a package. I created the following file DemoHelper.nuspec
3. Create a folder called lib under Demohelperpackage and copy RazorToMVCViaNuGet.dll to this location
4. copy nupack.exe to Demohelperpackage and run “NuPack.exe pack DemoHelper.nuspec” to generate the package
5. You would see the a RazorToMVCViaNuGet.1.1.nupkgnupkg file created in the same folder
Now that we have our package, we can test it out by having NuGet talk to a local feed rather than having NuGet talk to the official feed. You can refer how to do this from this page "Hosting Your Own Local and Remote NuPack Feeds"
Once you have this setup you can use the Package Manager Console to list and install the package that you had just created.
Create an ASP.NET MVC3 WebApplication to use this package in your project. Open the package manager console window.
To see the list of packages you can do list-package which would list out your package.
You can install the package using “Install-Package RazorToMVCViaNuGet” which would add the RazorToMVCViaNuGet.dll to your project
Once the package is installed a reference to RazorToMVCViaNuGet is added in your project
You can now use the helpers in RazorToMVCViaNuGet .dll in your views.Notice there is no namespace needed for the helpers.
When you run the application then Wola!!!! you would see that you were able to distribute and use your helpers 🙂
This shows how easy it is to compile your own library of Razor helpers which you can distribute using NuGet to MVC/WebMatrix developers
1. The sample only works for .cshtml file and can be extended for .vbhtml files as well
2. Nuspec for this helper can be extended to have a dependency on Microsoft.AspNet.WebPages to be installed.