[英]Why is json serialization generating errors when calling WebAPI?
為了演示該問題,我在由 Blazor WASM 解決方案模板創建的解決方案中添加了一些 POST 方法。 有些工作,有些不工作,產生我不理解的錯誤,我希望有人可以教育我。
這是客戶端代碼:
protected override async Task OnInitializedAsync()
{
try
{
// From the Blazor template
forecasts = await Http.GetFromJsonAsync<WeatherForecast[]>("WeatherForecast");
var w = new WeatherForecast();
// Works
await Http.PostAsJsonAsync<WeatherForecast>("WeatherForecast", w);
MemoryStream s = new MemoryStream();
// Generates exception: 'Timeouts are not supported on this stream.'
await Http.PostAsJsonAsync<MemoryStream>($"WeatherForecast/stream", s);
List<MemoryStream> l = new List<MemoryStream>() { new MemoryStream() };
// Generates exception: 'Timeouts are not supported on this stream.'
await Http.PostAsJsonAsync<List<MemoryStream>>($"WeatherForecast/list", l);
List<MemoryStream> m = new List<MemoryStream>();
// Works
await Http.PostAsJsonAsync<List<MemoryStream>>($"WeatherForecast/list", m);
// PdfDocument is part of the PdfSharp library which I'm using to build pdf
// files but I need to send it into the API to save it on a server.
PdfDocument doc = new PdfDocument();
// Generates exception: 'System.Security.Cryptography.Algorithms is not
// supported on this platform.'
await Http.PostAsJsonAsync<PdfDocument>("WeatherForecas/pdf", doc);
}
catch (Exception e)
{
throw;
}
}
這是服務器端 API 代碼:
[HttpPost]
public void Post([FromBody] WeatherForecast w)
{
if (w == null) return;
}
[HttpPost("stream")]
public void Post([FromBody] MemoryStream s)
{
if (s == null) return;
}
[HttpPost("list")]
public void Post([FromBody] List<MemoryStream> s)
{
if (s == null) return;
}
[HttpPost("pdf")]
public void Post([FromBody] PdfDocument doc)
{
if (doc == null) return;
}
}
評論顯示了哪些調用有效,哪些無效,對於那些沒有的,這些錯誤對我來說沒有意義。 誰能幫我理解為什么我不能將 PdfDocument 發送到 API?
在此先感謝您的幫助。
生成異常:“此平台不支持 System.Security.Cryptography.Algorithms。”
這是一個已知問題: System.Security.Cryptography APIs not supported on Blazor WebAssembly 。 目前沒有好的解決方法可以建議。
因此,作為一種解決方法,您可以嘗試讀取 pdf 內容並將內容作為字符串類型發送,或者將 pdf 文件發送到 API 方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.