What's the difference if you use Nancy as a web framework? actually a lot.
There is nothing to learn in using the Nancy framework to add rest nodes and routes to a Web page, and the rest framework is the same. Nancy uses a consistent approach to expand through a number of modular components, making it more suitable for developing a complete Web site.
Nancy is great for providing static files and data-based views based on custom routing (like ASP. NET MVC), in fact, you can even define a reference alias or route to a specific static file that already exists, rather than dynamically generating one.
See the following example. Imagine that Nancy's bootstrapper (described later) has the following code
protected Override void configureconventions (nancyconventionsnancyconventions) {CONVENTIONS.STATICCONTENTSCONVENTIONS.ADD ( Staticcontentconventionbuilder.addfile ("/jquery"," Scripts/jquery-2.1.1.min.js"));}
Then I can refer to this file in the HTML page I added or in an attempt to
<script src="~/jquery"></script>
At first glance, this may not be like anything special, but what happens if you decide to use the new version of jquery in your site?
Considering traversing each page, template and view, change each "?/script/jquery-2.1.1.min.js" to "?/script/jquery-3.0.0.min.js" (or whatever the new version is), or replace a single row in a class and recompile, I know which one I prefer.
You can map the entire directory structure, not just a single file. In the demo of this book, you will find the following in the bootstrapper code:
Code Listing 2
protected override void Configureconventions (nancyconventionsnancyconventions) { 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");}
With four global rules set above, any request "/scripts" will point to the Scripts folder in the same directory as the Nancy compiler, and the same for "/fonts" and "/images".
The rules for processing "/" are redirected to a folder named pages, so requesting "/home.html" will look for HTML content in./pages/home.html.
I'll cover this in more detail when I discuss the views later, but now you know that by default you don't have to do any processing. You can create a folder called content and put everything in it;
What does it mean for developers who want to add Nancy to the project?
Well, that means you can install Nancy (no other modules) from NuGet, create a folder called content in your application, and then add Html,javascript, stylesheets, or other,??, F5 to this folder, and start hosting the content.
Note that this content folder is not just for ASP. NET hosting, but also for WCF hosting, self-hosting in Windows services, or hosted in Azure. The actual host platform is not important at all; This is a default convention for Nancy.
This is just one of the tips Nancy offers to build a complete website system.
Nancy also offers:
- Dynamic attempt based on multi-view engine (dynamically views via multiple view engines)
- Simple model Binding
- Content and data validation (validation)
- Basic Conversation Management (Session management)
In this chapter, you learned about the features that Nancy applies to building an entire Web site and Web application, and in fact, in writing this book so far, I've actually heard some people call Nancyfx ". NET version of Nodejs "because it brings many of the features of node to the. NET technology stack.
In the next chapter, we will use the prefabricated Visual Studio templates to write the first Nancy-based Web application programmatically using "Super-duper-happy-path".
Chapter III Web Framework Nancy