DataContractJsonSerializer ReadObject Exception
- by Dan Appleyard
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