Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 邮政同步<;字符串>;can';t反序列化响应_C#_Asp.net Core - Fatal编程技术网

C# 邮政同步<;字符串>;can';t反序列化响应

C# 邮政同步<;字符串>;can';t反序列化响应,c#,asp.net-core,C#,Asp.net Core,我正在尝试从API获取字符串响应,但出现了一些错误: 以下是我的API方法: public class AuthenticationController : ApiController { public async Task<ActionResult<string>> PostAsync(CreateTokenCommand credentials) { string test= "eyJhbGciOiJIUzI1NiIsInR5

我正在尝试从API获取字符串响应,但出现了一些错误:

以下是我的API方法:

public class AuthenticationController : ApiController
{
    public async Task<ActionResult<string>> PostAsync(CreateTokenCommand credentials)
    {
        string test= "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9";

        return Ok(test);
    }
}
使用stacktrace:

位于System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack&ReadStack,JsonReaderException-ex) 位于System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions选项、Utf8JsonReader和reader、ReadStack和ReadStack) 位于System.Text.Json.JsonSerializer.ReadCore(输入returnType、JsonSerializerOptions选项、Utf8JsonReader&reader) 反序列化(字符串Json,类型returnType,JsonSerializerOptions选项) 反序列化[TValue](字符串Json,JsonSerializerOptions选项) 位于Microsoft.AspNetCore.Components.HttpClientJsonExtensions.d_u6
1.MoveNext(),位于System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(),位于System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务任务),位于System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务任务)at System.Runtime.CompilerServices.TaskAwaiter
1.GetResult() 在d:\MyProjects\CheckerApp\CheckerApp.WebUI\Services\JWTAuthenticationService.d\uu 3.MoveNext()中的CheckerApp.WebUI.Services.JWTAuthenticationService.cs:第38行


我在哪里犯了错误?如何修复它?

它希望收到一个JSON字符串,而“eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9”不是有效的JSON。可能
返回Ok(new{Test=Test})?在这种情况下,错误变成:“JSON值无法转换为System.String.Path:$|行号:0 | BytePositionLine:1。”因为PostJsonAsync等待字符串responceyah,因为它不再是
字符串
,而是一个对象。通常,您不会将
string
作为JSON的基础。它要么是一个对象,要么是一个arrayHmm,因此在这种情况下,除了创建dto对象来传输字符串值之外,没有其他方法了?好吧,在后端不需要它(因为匿名对象),在客户端可以使用
PostJsonAsync
,然后从对象获取“test”属性(我不记得确切的语法)。否则,不要为此使用JSON
public async Task Login(UserCredentials model)
    {

        try
        {
            var result = await _httpClient.PostJsonAsync<string>("https://localhost:44373/api/authentication", model);
            
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
'e' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0.