📜 ⬆️ ⬇️

We create the first application on NancyFX. Part Four We continue to work with the modules

In a previous article, Create the first application on NancyFX. Part Three Modules Nancy. We met with the modules Nancy. In this article we will continue their study.


As I mentioned in a previous article, Nancy uses the Request.Form request property. Form is a DynamicDictionary whose values ​​are taken from the request body. In order for Nancy to correctly parse the values ​​passed in the request body, you must correctly set the Content-Type: application / x-www-form-urlencoded. If the Content-Type was set incorrectly, do not be surprised if you will receive nulls as values. Automatically Nancy only parses data in x-www-form-urlencoded format. And so let's try to create a module processing Post request. First, create the following class:

namespace NancyFxApplication { public class Dish { public int Id { get; set; } public string Name { get; set; } public bool IsSpicy { get; set; } } } 

')
and class

 using System.Collections.Generic; namespace NancyFxApplication { public static class DishesRepository { public static List<Dish> Dishes = new List<Dish> { new Dish { Id=1, Name = "Porridge", IsSpicy = false}, new Dish {Id = 2, Name = "Chili", IsSpicy = true}, new Dish {Id = 3, Name = "Icecream", IsSpicy = false} }; public static List<Dish> GetAllDishes() { return Dishes; } public static void AddDish(Dish dish) { Dishes.Add(dish); } } } 


Next, we define a new module Nancy

 using Nancy; namespace NancyFxApplication { public class DishModule : NancyModule { public DishModule() : base("/Dish") { Post["/Add"] = ctx => { var dish = new Dish {Id = Request.Form.Id, Name = Request.Form.Name, IsSpicy = Request.Form.IsSpicy}; DishesRepository.AddDish(dish); return new Response { StatusCode = HttpStatusCode.Accepted }; }; Get["/GetAll"] = ctx => Response.AsJson(DishesRepository.GetAllDishes()); } } } 


Now after starting the application and navigating through the Dish / GetAll route, we get the following result
 [ {"Id":1,"Name":"Porridge","IsSpicy":false}, {"Id":2,"Name":"Chili","IsSpicy":true}, {"Id":3,"Name":"Icecream","IsSpicy":false} ] 


Now use the feeddler and make a Post request to the route Dish / Add



And again we make a Get request about the route of Dish / GetAll and get
 [ {"Id":1,"Name":"Porridge","IsSpicy":false}, {"Id":2,"Name":"Chili","IsSpicy":true}, {"Id":3,"Name":"Icecream","IsSpicy":false}, {"Id":1,"Name":"Borsh","IsSpicy":false} ] 


As you can see everything works. However, x-www-form-urlencoded is good only for simple data formats. But for complex data, it is not suitable. JSON and XML can help to transfer complex data formats. Let's modify the Dish class as follows:

 public class Dish { public int Id { get; set; } public string Name { get; set; } public bool IsSpicy { get; set; } public List<Ingridients> Ingridientses { get; set; } } 


Add a class to our project:
 public class Ingridients { public int Id { get; set; } public string Name { get;set; } } 


Nancy includes two desserializers for two types of content types. For JSON (“application / json”, “text / json”, “application / vnd ... + json”) and XML (“application / xml”, “text / xml”, “application / vnc ... + xml "). Nancy can also be extended to work with other types of content, such as Protocol Buffers, CSV, etc. You can even write your own serializer for your content type.
And so let's modify our module so that it can receive data in JSON format.

 using Nancy; using Nancy.ModelBinding; namespace NancyFxApplication { public class DishModule : NancyModule { public DishModule() : base("/Dish") { Post["/Add"] = ctx => { var dish = this.Bind<Dish>(); DishesRepository.AddDish(dish); return new Response { StatusCode = HttpStatusCode.Accepted }; }; Get["/GetAll"] = ctx => Response.AsJson(DishesRepository.GetAllDishes()); } } } 


Bind Method - an extension method that is in the Nancy.ModelBinding namespace. It allows you to map your JSON to your business object. Now if you start the application and execute the following POST request on the route "/ Add" in Fiddler



And then we execute Dish / GetAl then we get
 [ {"Id":1,"Name":"Porridge","IsSpicy":false,"Ingridientses":null}, {"Id":2,"Name":"Chili","IsSpicy":true,"Ingridientses":null}, {"Id":3,"Name":"Icecream","IsSpicy":false,"Ingridientses":null}, {"Id":12,"Name":"Salad","IsSpicy":false, "Ingridientses":[{"Id":1,"Name":"Salad"}]} ] 


As you can see, our model is accepted by the application and is recorded in our fake repository.
In conclusion, I want to talk about the desssrialization of the XML format. To use Bind, the properties of a business object must be marked with attributes as we do with regular XML serialization. At the same time, we will have a guarantee that the object will be correctly handled.

In the next article we will discuss with you the graphics engine Super Simple View Engine. Waiting for your comments and questions.

Source: https://habr.com/ru/post/200632/


All Articles