我正在尝试使用asp.net core(c#)在我的localhost(概念证明)环境中设置一个小服务器,但在访问'get'路由时仍然遇到同样的错误:
FormatException: Cannot determine type of resource to create from json input data. Is there a 'resourceType' member present? (at path 'line 1, pos 1')
这是我的代码:
[Route("Patient/rvrbk")]
public Patient ServeClient()
{
Patient patient = new Patient();
patient.Id = "1";
patient.Gender = AdministrativeGender.Male;
patient.Name = new List<HumanName> { new HumanName { Family = "Verbeek", Given = new List<string> { "Rik" }, Suffix = new List<string> { "The Small" } } };
patient.BirthDate = "2004-03-10";
patient.Active = true;
return patient;
}
[Route("get")]
public Patient GetClient()
{
FhirClient client = new FhirClient("http://localhost:54240/");
var patient = client.Read<Patient>("Patient/rvrbk");
return patient;
}
我尝试在患者对象上设置resourceType,但由于它只是readonly而产生错误。我确信我错过了一些微不足道的东西,却无法弄清楚是什么。
修改
在@AdrianoRepetti提供的答案之后,我提出了以下设置但遇到了另一个错误:
FhirOperationException: Operation was unsuccessful, and returned status OK. OperationOutcome: Overall result: FAILURE (1 errors and 0 warnings) [ERROR] (no details)(further diagnostics: Endpoint returned a body with contentType 'text/plain', while a valid FHIR xml/json body type was expected. Is this a FHIR endpoint?).
代码:
[Route("Patient/rvrbk")]
public string ServeClient()
{
Patient patient = new Patient();
patient.Id = "1";
patient.Gender = AdministrativeGender.Male;
patient.Name = new List<HumanName> { new HumanName { Family = "Verbeek", Given = new List<string> { "Rik" }, Suffix = new List<string> { "The Small" } } };
patient.BirthDate = "2004-03-10";
patient.Active = true;
FhirJsonSerializer serializer = new FhirJsonSerializer();
string json = serializer.SerializeToString(patient);
return json;
}
答案 0 :(得分:0)
我明白了。
我的第一个问题是(正如@Adriano Repetti所指出的那样)客户端不知道它正在接收哪个对象,这是通过FhirJsonSerializer提供的SerializeToString([object])函数运行对象来解决的。
我的下一个问题是我在客户端期望'application / json'时返回一个字符串。这是通过将'server'的返回类型更改为JsonResult并使用JObject.Parse([string])将生成的字符串解析为json来解决的。
<强>代码强>
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Hl7.Fhir.Model;
using Hl7.Fhir.Rest;
using Hl7.Fhir.Serialization;
using Newtonsoft.Json.Linq;
namespace FIHR.Controllers
{
[Route("")]
public class HomeController : Controller
{
public string Index()
{
return "Welkom op de FIHR API";
}
[Route("Patient/rvrbk")]
public IActionResult ServeClient()
{
Patient patient = new Patient();
patient.Id = "1";
patient.Gender = AdministrativeGender.Male;
patient.Name = new List<HumanName> { new HumanName { Family = "Verbeek", Given = new List<string> { "Rik" }, Suffix = new List<string> { "The Small" } } };
patient.BirthDate = "2004-03-10";
patient.Active = true;
FhirJsonSerializer serializer = new FhirJsonSerializer();
string jstring = serializer.SerializeToString(patient);
return Content(jstring, "application/json");
}
[Route("get")]
public Patient GetClient()
{
FhirClient client = new FhirClient("http://localhost:54240/"); // http://vonk.fire.ly
Patient patient = client.Read<Patient>("Patient/rvrbk"); // Patient/example
return patient;
}
}
}