Search code examples
c#asp.netjson-api

JSONAPI .NET post not functioning


I have a problem with JSONAPI .NET.

First of all, since there is no documentation for the JSONAPI .NET, I cannot be sure if I have all the configurations done correctly. Anyway, the case is as follows:

The WebApiConfig.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Web.Http;
using JSONAPI.Json;
using JSONAPI.Core;

namespace MyProjectWebApi
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
            JsonApiFormatter formatter = new JsonApiFormatter();
            formatter.PluralizationService = new PluralizationService();
            config.Formatters.Add(formatter);
            GlobalConfiguration.Configuration.Formatters.Clear();
            GlobalConfiguration.Configuration.Formatters.Add(formatter);

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}

I have custom class and a controller in MyClassController.cs:

namespace MyClassCreation
{

    [Serializable]
    public class MyClass
    {
        public string Param1 { get; set; }
        public string Param2 { get; set; }
        public string Param3 { get; set; }
        public string Param4 { get; set; }


        public MyClass(string param1, string param2, string param3, string param4)
        {
            Param1 = param1;
            Param2 = param2;
            Param3 = param3;
            Param4 = param4;
        }
    }

    public class MyController : JSONAPI.Http.ApiController<MyClass>
    {

        public HttpResponseMessage PostMyClass(MyClass newMyClass)
        {
            try
            {
                ...
            }
            catch (Exception x)
            {
                Messages.WriteLog(x);
                throw x;
            }
        }

        public IEnumerable<MyClass> GetMyClass(string value)
        {
            {
                List<MyClass> result = new List<MyClass>();
                result.Add(new MyClass(value, "2", "3", "4"));
                return result;
            }
            catch (Exception x)
            {
                Messages.WriteLog(x);
                throw x;
            }
        }
    }
}

The problem is that the GET works but the POST does not. I have used Postman to generate the calls to the server. Get goes through just fine and returns what it is supposed to. Put the POST is never directed to any route. The server notices the call but the method for catching the POST-call does not.

I also wrote the default constructor for the MyClassController. The code goes there on the debugger but after that not to the PostMyClass method.

I also tried several attributes for the method, such as [HttpPost] and [Route(...)].

The funny thing is that when I inherit the MyClassController from the .NET ApiController-class directly, the POST works, but I don't recevice any of the JSON data, so I do not have any data to work with.

Any help?

Thanks!!!

EDIT:

Noticed that the JSONAPI .NET requires that its ApiController base-class' method has to be overwritten and used for catching the POST-call. So now I can receive the POST-call, but still the received data is null although the original data sent in the Body-part of the message should be a JSONAPI standard JSON object.

public override IList<MyClass> Post(IList<MyClass> postedObjs)
{
    return base.Post(postedObjs);
}

The data was:

{
    "data": [{
        "type": "myClass",
        "attributes": {
            "param1": "1",
            "param2": "2",
            "param3": "3",
            "param4": "4"
        }
    }]
}

So the problem is that the postedObj is null.


Solution

  • It seems you are using the 0.2.0 version of JSONAPI.NET which is the latest on NuGet. Unfortunately this version is quite out of date and does not comply with the current version of JSON API. The version on master follows the spec pretty closely, although I haven't had time to polish it up and make a release. There is some rough documentation here.