WebApi POST + [ISerializable] + JSON .NET

At work I have taken on the task of building a small utility web site for admin needs. Thing is I wanted it to be very self contained so I have opted for this

  • Self hosted web API
  • JSON data exchanges
  • Aurelia.IO front end
  • Raven DB database

So I set out to create a nice web api endpoint like this

private IDocumentStore _store;

public LoginController(IDocumentStore store)
	_store = store;

public IHttpActionResult Post(LoginUser loginUser)

Where I then had this datamodel that I was trying to post via the awesome AWEWSOME REST plugin for Chrome

using System;
namespace Model
    public class LoginUser
        public LoginUser()
        public LoginUser(string userName, string password)
            UserName = userName;
            Password = password;
        public string UserName { get; set; }
        public string Password { get; set; }
        public override string ToString()
            returnstring.Format("UserName: {0}, Password: {1}", UserName, Password);

This just would not work, I could see the endpoint being called ok, but no matter what I did the LoginUser model only the post would always have NULL properties. After a little fiddling I removed the [Serializable] attribute and it all just started to work.

Turns out this is to do with the way JSON.Net works when it see the [Serializable] attribute.

For example if you had this model

public class ResortModel
    public int ResortKey { get; set; }
    public string ResortName { get; set; }

Without the [Serializable] attribute the JSON output is:

    "ResortKey": 1,
    "ResortName": "Resort A"

With the [Serializable] attribute the JSON output is:

    "<ResortKey>k__BackingField": 1,
    "<ResortName>k__BackingField": "Resort A"

I told one of my collegues about this, and he found this article : http://stackoverflow.com/questions/29962044/using-serializable-attribute-on-model-in-webapi which explains it all nicely including how to fix it

Hope that helps, sure bit me in the Ass


4 thoughts on “WebApi POST + [ISerializable] + JSON .NET

  1. How did you like using Aurelia?

    I’ve been following your blog for many years because I used your Cinch library for WPF. Thank you for that! A few years ago I moved into web development (and totally stopped doing desktop software development) and I’m a big fan of Aurelia (and its predecessor, Durandal), so it is cool to see you mention it! As you probably know, Aurelia also uses MVVM and has many familiar things such as value converters, the concept of a template part, etc. There are things I miss from WPF (lookless controls, excellent layout controls, etc.), but there are other things that are totally awesome, such as automatic observability — no need for INotifyPropertyChanged, etc. It would be great to hear your thoughts about it!

    • sachabarber says:

      I did indeed say Aurelia.

      I picked Aurelia as I like Rob (the main man behind this and Durandal), and I am not that into Angular (not even 2.0). I have done a lot of JS stuff (Angular, MVC, Node, React, Knockout etc etc) and really didnt dig angular that much, there is lots to like for sure, but for me Aurelia.IO seems a less “opinionated framework”. And coming from WPF it has many of the things I think were the good parts of WPF, such as binding, lookless controls (I think of web components standard as this really), converters, composition DI/IOC etc etc

      To be honest these days I am writing a lot of Scala, right now my split is 50% C# (WPF etc etc) and the other 1/2 Cassandra, Akka, Scala, Kafka. Common thing amongst all of that for me is RX which rocks in any language.

      In about 1 month I will be transitioning to full time API developer in Scala. Which I am looking forward to, to be honest, Scala is a very interesting language

      PS : For the layout : box model in CSS3, and the nice grid system in Bootstrap are ok I think

      • Aurelia definitely has some famous competition, but I see huge potential with Aurelia because of many great design choices (many of which you or I mentioned already). I think it is an exciting time to be a web developer!

        I think WPF was ahead of its time when it was first released (in design and also because of the resources required and that it was optimized for high-DPI). Now (more than a decade later), though, I feel like WPF has been mostly neglected by Microsoft and in the meantime, web development has had numerous advances (hardware-accelerated graphics, Node/NPM, TypeScript, etc., etc.), and the IT dept. where I work has rolled out IE11 to most users and also allow Chrome and Firefox.

        Regarding lookless controls, though, I’m thinking of how in WPF I was able to take a multi-select list box and completely change the look (for example, I changed the layout panel to a WrapPanel, and changed the ItemTemplate to render small thumbnails) but I still retained all of the multi-selection logic supporting Click, Ctrl+Click, Shift+Click, etc. plus full keyboard functionality such as arrow keys, Ctrl+A, Home, End, etc. and that gave me the multi-item picker I needed with very little effort. (I’m sure there is a blog how to do something similar on the web, but it most likely wouldn’t be as polished as the WPF version or would require a lot more effort.)

        Regarding layout, I used AvalonDock for WPF, which was pretty amazing, but nowadays, I don’t usually think we should try to make web sites look like desktop applications (especially for anyone that needs to support mobile devices or printing), so yeah Bootstrap plus some StickyTableHeaders works fairly well most of the time.

        Scala does look really interesting, so I will continue to read your blog posts about it, but my hope is that TypeScript will continue to rapidly evolve and be sufficient for my needs.

        And, yes RX rocks! I played with it a bit, and I’m sure I’ll be using it more in the future.

      • sachabarber says:

        Yeah I hear you about templating/lookless controls. Thanks for your comments. I was just wondering would you be willing to send me your email, in case I have any Aurelia queries. My email is my first name DOT my last name AT gmail DOT com

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: