0

I've been looking over the Internet but didn't find something relate to my problem.

I have visual studio 2017. Working in 4.6.1 .NET framework. The application is an asp.net MVC 4 application (standard).

I have a controler Dataset :

public class DatasetController : Controller
{
    [System.Web.Http.HttpPost]
    public JsonResult Push(dynamic data)
    {
        try
        {
            //here data has a value of {object} and is object typed
        }
        catch(Exception ex)
        {
        }
        return Json(null);
    }
}

The client can push any kind of data he wants. I will handle the structure from the dynamic object. I tried to switch to IDictionary type. But sub object still get the {object} value thing...

Here is the JSON sent (with content type as application/json) through Postman.

{
  data: {
    ApplicationName: "test",
    TestObject: {
      Name:"TestObject",
      TestInt: 42
    }
  }
}

My problem is I don't know how to deal with this kind of object. How can I retrieve information from ? I can't call :

var test = data.ApplicationName

It will throw a RuntimeBinderException with message "object" does not contain a definition for ApplicationName.

With strongly typed variables or interfaces I never ran into such a problem (bassically obvious...).

Please tell me if my post lack of information.

Thank you for your time.

EDIT : Added precisions about my problem and the Exception I could get trying to work with this object

2
  • 1
    check stackoverflow.com/a/17050505/2810015 if it solved your problem. Commented May 9, 2017 at 10:32
  • 1
    I works. Answering my question quoting your post. Commented May 9, 2017 at 10:39

1 Answer 1

1

As Nimish comment on the question. Here is a post answering my problem : Passing dynamic json object to C# MVC controller

The problem is coming from the Json.Net serializer not used by default by the Controller serializer. So we create value provider

public sealed class JsonDotNetValueProviderFactory : ValueProviderFactory
{
    public override IValueProvider GetValueProvider(ControllerContext controllerContext)
    {
        if (controllerContext == null)
            throw new ArgumentNullException("controllerContext");

        if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
            return null;

        var reader = new StreamReader(controllerContext.HttpContext.Request.InputStream);
        var bodyText = reader.ReadToEnd();

        return String.IsNullOrEmpty(bodyText) ? null : new DictionaryValueProvider<object>(JsonConvert.DeserializeObject<ExpandoObject>(bodyText, new ExpandoObjectConverter()), CultureInfo.CurrentCulture);
    }
}

Then creating a Wrapper in order to strongly type the parameter :

public class JsonDynamicWrapper
{
    /// <summary>
    /// Dynamic json obj will be in payload.
    /// 
    /// Send to server like:
    /// 
    /// { payload: data }
    /// </summary>
    public dynamic payload { get; set; }
}

Then updating the Push method :

[System.Web.Http.HttpPost]
public JsonResult Push(JsonDynamicWrapper data)
{
    try
    {
        var test = data.payload.ApplicationName;
    }
    catch(Exception ex)
    {
    }
    return Json(null);
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.