DataContractJsonSerializer ReadObject Exception

Posted by Dan Appleyard on Stack Overflow See other posts from Stack Overflow or by Dan Appleyard
Published on 2010-04-06T18:04:19Z Indexed on 2010/04/06 18:13 UTC
Read the original article Hit count: 2153

I am following the accepted answer of ASP.NET MVC How to pass JSON object from View to Controller as Parameter. Like the original question, I have a simple POCO.

Everthing works fine for me up until the DataContractJsonSerializer.ReadObject method. I am getting the following exception:

Expecting element 'root' from namespace ''.. Encountered 'None' with name '', namespace ''.

 Public Overrides Sub OnActionExecuting(ByVal filterContext As ActionExecutingContext)
        If filterContext.HttpContext.Request.ContentType.Contains("application/json") Then
            Dim s As System.IO.Stream = filterContext.HttpContext.Request.InputStream

            Dim o = New DataContractJsonSerializer(RootType).ReadObject(s)

            filterContext.ActionParameters(Param) = o
        Else
            Dim xmlRoot = XElement.Load(New StreamReader(filterContext.HttpContext.Request.InputStream, filterContext.HttpContext.Request.ContentEncoding))
            Dim o As Object = New XmlSerializer(RootType).Deserialize(xmlRoot.CreateReader)
            filterContext.ActionParameters(Param) = o
        End If
    End Sub

Any ideas?

Thanks

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about JSON