left-icon

NancyFX Succinctly®
by Peter Shaw

Previous
Chapter

of
A
A
A

CHAPTER 4

Nancy as a Web Framework

Nancy as a Web Framework


So what differs with Nancy for use as a web framework? Quite a lot, actually.

The process of adding restful endpoints for your pages and routes does not change when using Nancy as a web framework, so in this respect, you really don't need to learn anything new. Instead, like with everything else that Nancy does, there are modular parts that you can include into the mix that enable features suitable for building a full-blown web site or application.

Nancy will happily serve up static files and data-based views (just like ASP.NET MVC) based on the routes you define. In fact, you can even go so far as to define an alias or route that refers to a specific static file that already exists, rather than dynamically generating one.

Take the following example. Imagine I have the following in my Nancy bootstrapper (you'll learn more about these later in the book):

protected override void ConfigureConventions(NancyConventions nancyConventions)

{

  Conventions.StaticContentsConventions.Add(

    StaticContentConventionBuilder.AddFile(

      "/jquery",

      "scripts/Jquery-2.1.1.min.js"));

}

I can then refer to this file in any HTML pages or views I produce using the following:

<script src="~/jquery"></script>

At first glance, this might not seem like anything special, but what happens if you decide to update the version of JQuery you use in your site?

Given the choice between going through every page, template, and view, and changing every occurrence of "~/script/jquery-2.1.1.min.js" to "~/script/jquery-3.0.0.min.js" (or whatever the new version might be), or replacing a single line in one class and re-compiling that class, I know straightaway which one I'd prefer.

Don't forget that because this file mapping is defined within regular C# code, you can also now define that mapping using the same code, rather than specifying the two strings.

You could, for example, add a simple entry to your web.config file, which you then read and populate, allowing you to easily change the version in one place without recompiling, or you could even store the mapping in a database—the possibilities for managing this are endless.

It's not just single files you can map, either; you can map entire directory structures. In the demo app I've written to accompany this book, you'll find the following in the bootstrapper code:

Code Listing 2

protected override void ConfigureConventions(NancyConventions nancyConventions)

{

  Conventions.StaticContentsConventions.Add(

    StaticContentConventionBuilder.AddDirectory("/scripts", @"Scripts"));

  Conventions.StaticContentsConventions.Add(

    StaticContentConventionBuilder.AddDirectory("/fonts", @"fonts"));

  Conventions.StaticContentsConventions.Add(

    StaticContentConventionBuilder.AddDirectory("/images", @"Images"));

  Conventions.StaticContentsConventions.Add(

    StaticContentConventionBuilder.AddDirectory("/", @"Pages"));

}

What this does is set four global rules stating that any requests for "/scripts" go to a folder called scripts in the folder where the compiled Nancy binary is located. Likewise, the same is true for "/fonts" and "/images".

However, the rule to handle "/" is redirected to a folder called pages, so a request for "/home.html" will look in ./pages/home.html for the HTML content.

I'll cover this in more detail later when we discuss Views, but for now, know that by default you don't have to do any of this. You can create a folder called Content and put everything in there; Nancy will automatically look for and use that folder and its contents as a static file store if it exists.

What does this mean for the developer looking to add Nancy to his or her project?

Well, it means you can install Nancy (and no other modules) via NuGet, create a folder called Content in your application, start adding HTML, JavaScript, style sheets, and anything else you need to that folder, press the F5 key, and start hosting that content.

Take note that this Content folder does not just work with ASP.NET hosting. The same holds true even if you are hosting using WCF, self-hosting in a Windows service, or hosting on Azure. The actual hosting platform does not matter at all; this is a default Nancy convention that “just works.”

This is not the only trick Nancy offers to help with a full-blown web app.

Nancy provides:

  • Dynamic views via multiple view engines
  • Simple model binding
  • Content and data validation
  • Basic session management

Out of the box, and just as is possible with the restful features, everything is open and extendable via one of the many public interfaces the framework exposes, should you wish to extend it.

Summary

In this chapter, you learned about some of the features that make Nancy an ideal choice for building entire websites and web applications. In fact, while writing this book so far, I've actually heard a number of people refer to NancyFX as the "NodeJS of .NET" because it brings many of the features found in Node to the .NET stack.

In the next chapter, you will get to write your first Nancy-based web application using yet more of the "super-duper-happy-path," via the use of the Nancy projects’ pre-made Visual Studio templates.

Scroll To Top
Disclaimer
DISCLAIMER: Web reader is currently in beta. Please report any issues through our support system. PDF and Kindle format files are also available for download.

Previous

Next



You are one step away from downloading ebooks from the Succinctly® series premier collection!
A confirmation has been sent to your email address. Please check and confirm your email subscription to complete the download.