System.NotSupportedException:..带有.net和c#的API HTTPPost



我是一名工程系学生,正在做基于xamarin应用程序的期末学位项目,包括客户端(xamarin(和API(.net(之间的连接。我正在尝试发送json对象中包含的一些加密数据(以base64编码(作为请求。在API方面,它接收json请求,执行一些全同构加密函数,并在新的加密信息中返回响应。

问题是当我试图在API中接收作为名为"的自我创建类的响应时;Post.cs";其包括下一个属性;

public class Post
{
public ulong ? userId { get; set; }
public int ? id { get; set; }
public string? title { get; set; }
public string? body { get; set; }
public string? userIdEncrypted { get; set; }
public string? userIdEncryptedReturned { get; set; }
public string? parmsStr { get; set; }

public Post(ulong? userId, int? id, string? title, string? body, string? userIdEncrypted, string? userIdEncryptedReturned, string? parmsStr)
{
this.userId = userId;
this.id = id;
this.title = title;
this.body = body;
this.userIdEncrypted = userIdEncrypted;
this.userIdEncryptedReturned = userIdEncryptedReturned;
this.parmsStr = parmsStr;
}

因此,我的API接受该请求并对其进行反序列化;张贴";并用它做一些事情。我正试图复制HttpPost如下:

[Route("api/[controller]")]
[ApiController]
public class PostController
{

#region CONSTRUCTOR

public PostController()
{
}

#endregion

//POST ://api/Post
[Route("api/[controller]")]
[HttpPost]
public Post ReceivePost([FromBody] Post post)
{
...
var _post = new Post(post.userId, post.id, post.title, post.body, post.userIdEncrypted
post.userIdEncryptedReturned, post.parmsStr);

... FHE functions...
return _post;
}
}

因此,在我发布";张贴";从xamarin上的客户端,我发送了一个Post作为前面提到的结构,其中userIdEncrypted和parmsStr包含一个base64编码的字符串。当它到达API服务器时,会出现以下问题:

Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
An unhandled exception has occurred while executing the request.
System.NotSupportedException: Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported. Type 'System.IO.Stream'. Path: $ | LineNumber: 0 | BytePositionInLine: 1.
---> System.NotSupportedException: Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported. Type 'System.IO.Stream'.
--- End of inner exception stack trace ---

XAMARIN应用程序上的客户端这是我从客户端发布的json字符串:

PostModel postAux = new PostModel()
{
userId = 2,
id = 1,
title = "Title for post 1",
body = "Body for post 1",
};


/******************************************************
* Encrypt data of the post (userId)
******************************************************/
PostModel newPost = initializeFHE(postAux);
//Here is where I fill the encrypted data (base64 string) included in the Post object

/******************************************************
* POST encrypted data to the server in csharp
******************************************************/

Uri requestUri = new Uri("http://myHost:3000/api/Post");
var client = new HttpClient();

client.DefaultRequestHeaders
.Accept
.Add(new MediaTypeWithQualityHeaderValue("application/json")); // ACCEPT header
var json = JsonConvert.SerializeObject(newPost);
var contentJson = new StringContent(json.ToString(), Encoding.UTF8, "application/json");
//Console.WriteLine(contentJson);
var response = await client.PostAsync(requestUri, contentJson);
...

其中PostModel指的是这种自行创建的模型:

public class PostModel : BaseViewModel
{
public ulong userId { get; set; }
public int id { get; set; }
public string  title { get; set; }
public string  body { get; set; }
public string  userIdEncrypted { get; set; }
public string  userIdEncryptedReturned { get; set; }
public string  parmsStr { get; set; }
}

我知道我在.Net和c#上编程经验不足,所以欢迎任何帮助和解释。

谨致问候,劳尔。

这是一条罕见的错误消息,告诉您问题是什么

不支持对没有无参数构造函数、奇异参数化构造函数或用"JsonConstructorAttribute"注释的参数化构造函数的类型进行反序列化。

您需要向Post添加一个默认构造函数

public Post() {}

相关内容

  • 没有找到相关文章

最新更新