首页 > 解决方案 > 如何在.net core web api服务器上处理从xamarin发送的请求

问题描述

我尝试使用 HttpClient 将带有序列化数据的请求发送到我的服务器

var content = JsonConvert.SerializeObject(note);
var response = await _client.PostAsync(url, new StringContent(content));

这是我在控制器中的方法:

[HttpPost]
public ActionResult<Note>Create([FromBody]note)
{
    _noteService.Create(note);
    return new Note();//CreatedAtRoute("GetBook", new { id = note.Id.ToString() }, note);
}

我得到错误Unsupported MediaType,我试图将参数“note”数据类型更改为StringContent并且我得到“ Bad Gateway ”错误,我试图将其更改为String数据类型并且它是空的。

如何从我的服务器上的 xamarin 应用程序获取数据发送?

编辑:可能我必须获取序列化字符串并将其反序列化为我的对象。

标签: c#xamarinasp.net-core

解决方案


已解决,请查看以下解决方案

Xamarin 代码

 var content = JsonConvert.SerializeObject(note);
 var response = await _client.PostAsync(url, new StringContent(content, Encoding.UTF8, "application/json"));

.Net Core WebApi 代码

[HttpPost]
 public ActionResult<Note>Create(Note note)
 {
    _noteService.Create(note);
    return CreatedAtRoute("GetNote", new { id = note.Id.ToString() }, note);
 }

推荐阅读