Home .NET Creating the first application on NancyFX. Part three. Nancy modules

Creating the first application on NancyFX. Part three. Nancy modules

by admin

In a previous article on NanacyFX Creating your first app on NancyFX. Part two. Bootstrapper We got acquainted with the built-in NancyFX TinyIoC and learned how to use it. In this article we will touch on Nancy modules in details.
So, as we have seen, the interaction with the Nancy application is a typical scenario for http sessions. That is, a request is sent by the user to the server, the server receives the request, the application processes the request, generates a response, and the server returns that response to the user. Let’s now take a closer look at how this sequence of actions applies to Nancy projects.
Module in Nancy defines a root that contains http method + processing method pattern + additional conditions
Creating the first application on NancyFX. Part three. Nancy modules
A module in Nancy is a container class whose main purpose is to define rota. Plus the class contains many useful properties and extensible methods. In the first article Creating your first application with NancyFX We created the Nancy module below.
Creating the first application on NancyFX. Part three. Nancy modules
We have defined a module with a Get method, with a root pattern that points us to the root of our application. Next, we have defined the return response. Now let’s figure out how Nancy determines which router to choose and how the http request is mapped to the nancy.request object.
Creating the first application on NancyFX. Part three. Nancy modules
The first line of the http request is mapped to the Method, Path, Query and Url properties. The request header parameters are mapped to the Cookies, Headers and Session properties. The request body is mapped to properties Body, Files, Form. Therefore, for the request from figure 3 will be defined a Post method with pattern "/orders". So here are some rules which Nancy uses to choose which method with a certain routing.

  • Modules can be loaded in a different order each time the application starts. That is, if you define two identical methods with the same basic rota patterns in different modules, you cannot tell for sure which one will be executed when the application is invoked.
  • Routes are used in modules in reverse order of what they were defined to be. This means that if you define two identical methods with the same root patterns in the same module, only the last defined method implementation will be used.
  • Exact matches take precedence over inexact matches.
  • The method with the highest number of matches in the pattern will be chosen
  • If more than one method was selected, the last one selected will be used

So let’s create such a module that when we access the same route, the application will redirect us either to WebAPI or to the Hello World page we created in the first article. And create in our first application on Nancy new module RouteModule, where we will define two identical routes "/client". The code of the route will look like this

using Nancy;namespace NancyFxApplication{public class RouteModule : NancyModule{public RouteModule(){Get["/client"] = p => "Hello API user!";Get["/client"] = p => View["Index.html"];}}}

When launching the application and calling the /client root we will see the following page.
Creating the first application on NancyFX. Part three. Nancy modules
If we use Fiddler for the query we see the following picture
Creating the first application on NancyFX. Part three. Nancy modules
As you see, according to the rules the last method is used by default. Now let’s modify the method so that WebAPI would be called when Fiddler calls the router. To do this, change the class code as follows.

using System;using Nancy;namespace NancyFxApplication{public class RouteModule : NancyModule{private readonly Func<Request, bool> _isNotApiClient = request => !request.Headers.UserAgent.Equals("Fiddler");public RouteModule(){Get["/client"] = p => "Hello API user!";Get["/client", context => _isNotApiClient(context.Request)] = p => View["Index.html"];}}}

Once the application is running, let’s try calling it with the browser. The same Hello World page will be returned to us. However, if the same router is called with Fiddler then we will see the following :
Creating the first application on NancyFX. Part three. Nancy modules
Our application will use the first root as we check the client name in our filter function.
Finally, I would like to add that the nancy.response object is mapped to an http response in the same way that an http request is mapped to a nancy.request object.
Creating the first application on NancyFX. Part three. Nancy modules
The first line of the response header is mapped to the StatusCode property. As with the http request, the headers of the http response are mapped to the ContentType, Cookies and Headers properties, and the response body is mapped to the Content property.
And I can’t resist announcing the next article. In it we will continue to work with Nancy’s modules.
I’m waiting for your comments and questions about this opus.

You may also like