您好,我一直在尝试解决此问题。我有一个WebApi,它接受Json字符串作为POST。客户端正在访问我们的Webapi。但是,“结果” 始终为NULL。我已经研究了几天,但找不到答案。我正在使用Postman将响应发布到WebApi。我是WebApi的新手,这是我的第一篇文章。希望有人能对此有所启发。谢谢
WebApiConfig.cs
public static void Register(HttpConfiguration config)
{
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new
HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
config.Filters.Add(new BasicAuthenticationFilterAttribute());
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}"
);
}
Global.asax.cs
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
OcrDataReceiverController.cs -“结果”始终为NULL
[RoutePrefix("api/OcrDataReceiver")]
[Authorize]
public class OcrDataReceiverController : ApiController
{
[HttpPost]
[AllowAnonymous]
public HttpResponseMessage Post([FromBody]OcrResultModel result)
{
// Do Something
}
}
OcrResultModel.cs
[Serializable]
public class OcrResultModel
{
public string requestId { get; set; }
public string customerRequestId { get; set; }
}
由于我是stackoverflow的新手,所以我还不能发布任何图像(邮递员)。在邮递员中,我有Content-Type = application / json。
正文:原始-JSON(application / json)
{"requestId":"2b20cba5-6715-4d3e-aea6-9e72088d87bc","customerRequestId":"Joseph123d"}
答案 0 :(得分:0)
从课程中删除Serializable
。它会起作用。
由于Json.NET序列化程序默认将IgnoreSerializableAttribute
设置为true,因此它将忽略属性
public class OcrResultModel
{
public string requestId { get; set; }
public string customerRequestId { get; set; }
}