【问题标题】:JSONAPI .NET post not functioningJSONAPI .NET 帖子无法正常工作
【发布时间】:2015-09-21 21:24:12
【问题描述】:

我对 JSONAPI .NET 有疑问。

首先,由于没有 JSONAPI .NET 的文档,我无法确定我是否正确完成了所有配置。反正情况如下:

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 }
            );
        }
    }
}

我在 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;
            }
        }
    }
}

问题是 GET 有效,但 POST 无效。我使用 Postman 生成对服务器的调用。 Get 处理得很好并返回它应该做的。将 POST 永远不会定向到任何路由。服务器注意到调用,但捕获 POST 调用的方法没有。

我还为 MyClassController 编写了默认构造函数。代码会在调试器上出现,但之后不会出现在 PostMyClass 方法中。

我还尝试了该方法的几个属性,例如[HttpPost]和[Route(...)]。

有趣的是,当我直接从 .NET ApiController 类继承 MyClassController 时,POST 工作,但我没有收到任何 JSON 数据,所以我没有任何数据可以使用。

有什么帮助吗?

谢谢!!!

编辑:

注意到 JSONAPI .NET 要求其 ApiController 基类的方法必须被覆盖并用于捕获 POST 调用。所以现在我可以接收到 POST 调用,但接收到的数据仍然为空,尽管消息正文部分中发送的原始数据应该是 JSONAPI 标准 JSON 对象。

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

数据是:

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

所以问题是postedObj为空。

【问题讨论】:

  • 您用于 POST 的 JSON 看起来如何?将默认构造函数添加到您的类MyClass。我认为这是必需的。您还应该使用DataContractAttribute 和DataMemberAttribute 而不是SerializableAttribute
  • 我认为默认的构造函数无论如何都是由构建实现的。 JSON 是:{"MyClass":{"Param1":"1","Param2":"2","Param3":"3","Param4":"4"}}

标签: c# asp.net json-api


【解决方案1】:

看来您使用的是 0.2.0 版本的 JSONAPI.NET,它是 NuGet 上的最新版本。不幸的是,这个版本已经过时了,不符合 JSON API 的当前版本。 master 上的版本非常接近规范,虽然我还没有时间完善它并发布。有一些粗略的文档here。

【讨论】:

  • 谢谢!我实际上同时意识到了这一点,并将JSON数据修改为以下格式:{ "myClasss": { "type": "myClass", "id": "1", "param1": "1", "param2": "2", "param3": "3", "param4": "4" } } //MyClasss is supposed to be plural of the myClass
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多