c# - 如何在.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 应用程序获取数据发送?
编辑:可能我必须获取序列化字符串并将其反序列化为我的对象。
解决方案
已解决,请查看以下解决方案
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);
}
推荐阅读
- gradle - 无法将发布“mavenJava”发布到存储库“maven”
- php - PHP class_exists() 找不到真正存在的类
- asp.net-mvc - 指定实现时带有接口类的 WebAPI 模型 [ModelBinder]
- python - How to get ckeditor field value in modelform after submit in Django2.1?
- ms-access - 连接 Power BI 以通过网络访问数据库
- javascript - 直方图:所有 bin 的长度为 0
- android - 进程可以在android中共享变量吗?
- angular - Angular7 - 使用去抖动器时视图不会更新
- node.js - 在 pupeteer Node.js 中获取 href 属性
- prism - 我们可以让一个 .exe 使用 XAML 和 Prism(用于 MVVM)调用 .dll 吗?