JSONAPI .NET post 未运行
JSONAPI .NET post not functioning
我对 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 }
);
}
}
}
我有自定义 class 和控制器 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 顺利通过,returns 它应该做什么。把 POST 从不定向到任何路由。服务器注意到调用,但捕获 POST 调用的方法没有。
我还为 MyClassController 编写了默认构造函数。代码在调试器上进入那里,但之后不会进入 PostMyClass 方法。
我还尝试了该方法的几个属性,例如 [HttpPost] 和 [Route(...)]。
有趣的是,当我直接从 .NET ApiController-class 继承 MyClassController 时,POST 有效,但我没有收到任何 JSON数据,所以我没有任何数据可以使用。
有什么帮助吗?
谢谢!!!
编辑:
注意到 JSONAPI .NET 要求其 ApiController base-class' 方法必须被覆盖并用于捕获 POST-调用。所以现在我可以接收到 POST-call,但接收到的数据仍然为空,尽管在消息正文部分发送的原始数据应该是 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为null。
看来您使用的是 JSONAPI.NET 的 0.2.0 版本,这是 NuGet 上的最新版本。不幸的是,这个版本已经过时了,不符合 JSON API 的当前版本。 master 上的版本非常接近规范,尽管我还没有时间完善它并发布。有一些粗略的文档 here.
我对 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 }
);
}
}
}
我有自定义 class 和控制器 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 顺利通过,returns 它应该做什么。把 POST 从不定向到任何路由。服务器注意到调用,但捕获 POST 调用的方法没有。
我还为 MyClassController 编写了默认构造函数。代码在调试器上进入那里,但之后不会进入 PostMyClass 方法。
我还尝试了该方法的几个属性,例如 [HttpPost] 和 [Route(...)]。
有趣的是,当我直接从 .NET ApiController-class 继承 MyClassController 时,POST 有效,但我没有收到任何 JSON数据,所以我没有任何数据可以使用。
有什么帮助吗?
谢谢!!!
编辑:
注意到 JSONAPI .NET 要求其 ApiController base-class' 方法必须被覆盖并用于捕获 POST-调用。所以现在我可以接收到 POST-call,但接收到的数据仍然为空,尽管在消息正文部分发送的原始数据应该是 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为null。
看来您使用的是 JSONAPI.NET 的 0.2.0 版本,这是 NuGet 上的最新版本。不幸的是,这个版本已经过时了,不符合 JSON API 的当前版本。 master 上的版本非常接近规范,尽管我还没有时间完善它并发布。有一些粗略的文档 here.