Update: The options for setting up and hosting a local NuGet server have changed. Please read the updated article.
Last week, Phil Haack wrote a post on how to build a NuGet Server. A NuGet server is really easy to build because 90% of the work is done by running a NuGet command. Just for fun, I decided to add some extra functionality to my NuGet Server such as a package upload form and a formatted list of packages.
In preparing my NuGet server, I followed Phil’s direction to a tee except for the fact that I used a “empty MVC 3 app with Razor support” instead of using a “ASP.NET Empty Web Application”. Out of the box, you will get a barebones, but fully functional NuGet server that provides the package list in Atom Format. For each package in the repository you will get access to properties such as the version number, license information, tags and etcetera. However, since I am not a fan of reading XML I thought it would be useful to transform that XML into HTML using XSLT. This way I can see the list of available packages without opening up Visual Studio. XSLT is one of those technologies that I have always read about but never used. So in the spirit of learning I decided to give it a try.
XSLT stands for Extensible Stylesheet Language Transformation. The basic concept is to take a XML file and apply a series of transformations to the underlying data which produces a human-readable HTML file. In order to get this working, I leveraged Tomas Peczek’s XmlActionResult class. Since Tomas did all the hard work, it’s just a matter of loading an XML doc, assigning it to an XmlActionResult and specifying the Transform Source:
public ActionResult PackageList()
var doc = new XmlDocument();
string path = GetUrl("/nuget/Packages").ToString();
var result = new XmlActionResult();
result.Document = doc;
result.TransformSource = Server.MapPath("~/Content/xsl/packages.xsl");
From the code snippet above, you can see that I created a XML transformation file named packages.xsl. I then applied the XSL file to the “Atom” package feed. Unfortunately, learning XSLT is a subject too large for me to try to cover in a single blog post. However, the basic concepts are simple and I was able to come up with a working prototype in about 10 minutes. If you are new to XSL, I would recommend reading he tutorials at W3Schools. Also notice, that I added the ChildActionOnly attribute to my controller method. This means that this content can only be called by using the Action or RenderAction HTML Helper method. Since I am using a master page in my solution, I only render the inline HTML markup for part of the view instead of a complete HTML document. Here is the markup for the view which displays the package list. As you can see, it’s just a dead simple Razor page that uses the Html.Action helper method to inject the HTML returned by the PackageList method into the view.
ViewBag.Title = "Index";
Here is a screenshot of the Package list page. You may notice that one of the packages below is an Orchard widget that I created. I will be blogging more about Orchard development in the near future so stay tuned…
Besides a formatted package list I also added an upload form. This was a simple task of taking the uploaded file and saving it to the packages directory of the web site. Here is a screenshot: