
[英]Implement DelegatingHandler in ASP.NET Core 5.0 Web API?
[英]Error in ASP.NET Web API Core 5.0 with C# client
我正在尝试在 ASP.NET Core 5.0 中创建一个 HTTP POST Web API,在其中我可以在字典变量PostValues
中的键/值对中接收任何数据作为字符串,如字符串、Int 作为字符串或二进制、图像作为 Base64:
网络API:
public class WebserviceController : ControllerBase
{
[HttpPost]
[Consumes("application/json")]
[Produces("application/json")]
public String Post([FromBody] Dictionary<String, String> PostValues)
{
return JsonConvert.SerializeObject(new ClassResponse(true, PostValues));
}
}
客户:
RestClient client = new RestClient("https://localhost:44395/webservice");
RestRequest request = new RestRequest(Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddHeader("Content-Type", "application/json");
request.AddParameter("Key1", "Value1");
request.AddParameter("Key2", "Value2");
IRestResponse response = client.Post<String>(request);
Response = JsonConvert.DeserializeObject<ClassResponse>(response.Content);
传递/返回数据的通用类:
public class ClassResponse
{
public ClassResponse(Boolean Success, Object Result)
{
this.Success = true;
this.Result = Result;
}
public Boolean Success;
public Object Result;
}
但是我在response.Content
收到错误(不是异常): {"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":400,"traceId":"00-7462e05ef46bf946bc7d102a93cc479e-06107777372f8f46-00","errors":{"$":["'w' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0."]}}
和response.StatusCode
为: System.Net.HttpStatusCode.BadRequest
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.