.NET Zone is brought to you in partnership with:

Michael Ceranski is a .NET developer from Buffalo NY. He has been writing code for over 10 years starting with Borland Delphi and later migrating to the .NET stack. Michael enjoys blogging about .NET, MVC and jQuery. When Michael is not consulting he spends his time working on WeBlog, a next generation blogging platform written in ASP.NET MVC 2. Michael is a DZone MVB and is not an employee of DZone and has posted 34 posts at DZone. You can read more from them at their website. View Full User Profile

Building a Nuget Server with gold plating

04.05.2011
| 6390 views |
  • submit to reddit

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:

[ChildActionOnly]
public ActionResult PackageList()
{
    var doc = new XmlDocument();
    string path = GetUrl("/nuget/Packages").ToString();
    doc.Load(path);
    var result = new XmlActionResult();
    result.Document = doc;
    result.TransformSource = Server.MapPath("~/Content/xsl/packages.xsl");
    return result;
}

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";
}

<h4>Packages</h4>

@Html.Action("PackageList")

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…

nuget-packagelist

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:

nuget-index

If you are interested in using this code for your own purposes you can download the full source code here via BitBucket. If you want to add any cool features to the server then I will be happy to grant access to the repository. Enjoy!

References
Published at DZone with permission of Michael Ceranski, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)