Does NancyFx bind a model to a dynamic type?

In Nancy, is there a way to associate the contents of a POST request with a dynamic type?

For example:.

// sample POST data: { "Name": "TestName", "Value": "TestValue" } // model class public class MyClass { public string Name { get; set; } public string Value { get; set; } } // NancyFx POST url Post["/apiurl"] = p => { // this binding works just fine var stronglyTypedModel = this.Bind<MyClass>(); // the following bindings do not work // there are no 'Name' or 'Value' properties on the resulting object dynamic dynamicModel1 = this.Bind(); var dynamicModel2 = this.Bind<dynamic>(); ExpandoObject dynamicModel3 = this.Bind(); var dynamicModel4 = this.Bind<ExpandoObject>(); } 
+8
c # model-binding nancy
source share
4 answers

Outside the box, Nancy does not support dynamic model binding. TheCodeJunkie wrote a quick ModelBinder to achieve this.

https://gist.github.com/thecodejunkie/5521941

Then you can use it like this:

dynamic model = this.Bind<DynamicDictionary>();

+9
source share

As in previous answers, there is no support for binding directly to a dynamic type, the most similar is the ModelBinder provided by TheCodeJunkie at https://gist.github.com/thecodejunkie/5521941

However, this approach has a problem and lies in the fact that the DynamicDictionary obtained as a result of this code does not order later, producing only dictionary keys and losing values. This is described here. Why does storing Nancy.DynamicDictionary in RavenDB save property names, not property values? and today (version 1.4.3) is still happening, seriously limiting this approach.

The solution is to use a simple trick, access to the raw data received in POST, and deserialize using JSON.Net. In your example, this would be:

 using System; using System.Dynamic; using Nancy; using Nancy.Extensions; using Newtonsoft.Json; Post["/apiurl"] = p => { dynamic obj = JsonConvert.DeserializeObject<ExpandoObject>(Request.Body.AsString()); //Now you can access the object using its properties return Response.AsJson((object)new { a = obj.Prop1 }); } 

Note that you need to use Nancy.Extensions to call Request.Body.AsString ().

+1
source share

I was looking for a way to deserialize my POST body as dynamic and found this question, I put my solution using Newtonsoft and the extension method in case the result is useful to someone else.

Extension method

 using System.IO; using Nancy; using Newtonsoft.Json; namespace NancyFx { public static class DynamicModelBinder { public static dynamic ToDynamic(this NancyContext context) { var serializer = new JsonSerializer(); using (var sr = new StreamReader(context.Request.Body)) { using (var jsonTextReader = new JsonTextReader(sr)) { return serializer.Deserialize(jsonTextReader); } } } } } 

Using

 using Nancy; using Nancy.ModelBinding; namespace NancyFx { public class HomeModule : NancyModule { public HomeModule(IAppConfiguration appConfig) { Post("/product", args => { dynamic product = Context.ToDynamic(); string name = product.Name; decimal price = product.Price; return Response.AsJson(new {IsValid=true, Message= "Product added sucessfully", Data = new {name, price} }); }); } } } 
0
source share

I'm not sure, but you can try:

 dynamic model = new ExpandoObject(); model = Request; //or Request.Form return View["ViewName", model]; 

let me know if it works :)

-one
source share

All Articles