[Mono-aspnet-list] asp.net mvc web api with mono: JSON results contain no null for nullable types

marcelbeck marcel.beck at taicanet.com
Thu Mar 12 18:21:43 UTC 2015


Hello,

i have a problem with mono and json results and i don´t find any solution
for it. I asked it at stack overflow and paste the text here. So, for a
better formatted version of the question, see:


http://stackoverflow.com/questions/29017201/asp-net-mvc-web-api-with-mono-json-results-contain-no-null-for-nullable-types


Hi,

i have a problem that drives me crazy. I build a asp.net mvc web api project
in visual studio 2013 on a windows 8.1 machine. It targets .net framework
4.0 (i choose this explicitly because it fits mono 3.2.8 perfect). I create
the project with visual studio empty template, and VS installed the
appropriate nuget packages for me.

Then i installed mono 3.2.8 on ubuntu 14.04 LTS via apt-get install
mono-complete. XSP is used as the web server. Nothing special done on both
systems or the project. When code-changes are done, i deploy in VS the
project to a folder and copy it to the ubuntu machine. Restart XSP and it
runs.

All works really fine since a long time (8 months and many code lines).

But now i had to change my model, used as the JSON transport object, so that
every bool or int is now nullable (bool? instead of bool). After i changed
the properties of the model, and run the web api on the windows machine per
F5, the JSON result in the browser (firefox or chrome, both show in every
cases always the same results) contains "MyBoolValue: null" whenever the C#
model property was null.

Perfect. Thats what i expected and what i want.

Now i copied the deploy to my ubuntu machine and let it run, as every time.
But in the browser result there are no nulls ! Every "bool?" which where
null in the model, not exists in the json result at all. Every "double?" is
set to "0.0". I dont understand that ???

So i tried to set the JSON SerializeSettings (but i think "including nulls"
is the default, so it is senseless):

        var f = new JsonMediaTypeFormatter();

        f.SerializerSettings.NullValueHandling =
Newtonsoft.Json.NullValueHandling.Include;

        config.Formatters.Add(f);

But that produced the following error on the asp.net engine:

System.MissingMethodException Method not found:
'System.Net.Http.Formatting.JsonMediaTypeFormatter.get_SerializerSettings'.

Description: HTTP 500.Error processing request. Details: Non-web exception.
Exception origin (name of application or object): HHS.June.WebApi. Exception
stack trace: at HHS.June.WebApi.MvcApplication.Application_Start ()
[0x00000] in :0 at (wrapper managed-to-native)
System.Reflection.MonoMethod:InternalInvoke
(System.Reflection.MonoMethod,object,object[],System.Exception&) at
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags
invokeAttr, System.Reflection.Binder binder, System.Object[] parameters,
System.Globalization.CultureInfo culture) [0x00000] in :0 Version
Information: 3.2.8 (Debian 3.2.8+dfsg-4ubuntu1); ASP.NET Version:
4.0.30319.17020

I tried it 6 ours, installing the Newtonsoft.JSON in the gac, changed
versions, tried some web.config things, google google google, try to
understand what and why the mono json serializer is doing what he is doing.

It drives me crazy. Is anybody out there who know or understand or have at
least an idea, how i can get a JSON response with "MyValue: null" when using
nullable types in c# under mono ?

Every tip is very very appreciated :)




--
View this message in context: http://mono.1490590.n4.nabble.com/asp-net-mvc-web-api-with-mono-JSON-results-contain-no-null-for-nullable-types-tp4665627.html
Sent from the Mono - ASP.NET mailing list archive at Nabble.com.


More information about the Mono-aspnet-list mailing list