繁体   English   中英

Asp.net Server application is working on localhost, but not in Azure (Unhandled exception rendering component: ExpectedJsonTokens)

[英]Asp.net Server application is working on localhost, but not in Azure (Unhandled exception rendering component: ExpectedJsonTokens)

我已经创建了一个 web 应用程序,这是我第一次将它带入 Azure。我已经按照所有步骤操作并获得了 SQL 以及所有连接字符串等等。 我可以从 web 地址访问我的应用程序。 但是,一旦我尝试从 web 登录或注册,我就会遇到异常。

我做了什么:

在我的 Visual Studio 解决方案中打开appsettings.json并将连接字符串添加到 Azure SQL。然后在我的计算机上以调试模式运行我的应用程序并尝试注册一个新用户=>一切正常:所以如果我从我的计算机访问 Azure SQL (https://localhost,7113/login) 一切正常。 我能够读取和写入数据库。 如果我从https://xxx.azurewebsites.net/login做同样的事情,我会收到下面提到的错误。 任何想法为什么会这样以及如何解决?

跟设置ap API路由有关系吗?

我的 launchSettings.jason:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:25391",
      "sslPort": 44337
    }
  },
  "profiles": {
    "MaDashWeb.Server": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://localhost:7113;http://localhost:5113",
      "dotnetRunMessages": true
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "Docker": {
      "commandName": "Docker",
      "launchBrowser": true,
      "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}",
      "publishAllPorts": true,
      "useSSL": true
    }
  }
}

我正在以这种方式构建解决方案 model:客户端、服务器、共享

暴击:Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] 未处理的异常渲染组件:ExpectedJsonTokens 路径:$ | 行号:0 | BytePositionInLine:0.System.Text.Json.JsonException:ExpectedJsonTokens 路径:$ | 行号:0 | BytePositionInLine:0。---> System.Text.Json.JsonReaderException:ExpectedJsonTokens LineNumber:0 | BytePositionInLine: 0. 在 System.Text.Json.ThrowHelper.ThrowJsonReaderException(Utf8JsonReader&, ExceptionResource, Byte, ReadOnlySpan 1 ) at System.Text.Json.Utf8JsonReader.Read() at System.Text.Json.Serialization.JsonConverter [JsonMDasherialization[JsonMDasherialization .Shared.Authorization.LoginResult, MDashWeb.Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].ReadCore(Utf8JsonReader&, JsonSerializerOptions, ReadStack& ) Exception_EndOfInnerExceptionStack 在 System.Text.Json.ThrowHelper.ReThrowWithPath(ReaderStack&, JsonReaderException) ) 在 System.Text.Json.Serialization.JsonConverter 1[[MDashWeb.Shared.Authorization.LoginResult, MDashWeb.Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].ReadCore(Utf8JsonReader&, JsonSerializerOptions, ReadStack& ) at System.Text.Json.JsonSerializer.ReadCore[LoginResult](JsonConverter, Utf8JsonReader&, JsonSerializerOptions, ReadStack& ) at System.Text.Json.JsonSerializer.ReadCore[LoginResult](JsonReaderState&, Boolean, ReadOnlySpan 1[[MDashWeb.Shared.Authorization.LoginResult, MDashWeb.Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].ReadCore(Utf8JsonReader&, JsonSerializerOptions, ReadStack& ) at System.Text.Json.JsonSerializer.ReadCore[LoginResult](JsonConverter, Utf8JsonReader&, JsonSerializerOptions, ReadStack& ) at System.Text.Json.JsonSerializer.ReadCore[LoginResult](JsonReaderState&, Boolean, ReadOnlySpan 1, JsonSerializerOptions, ReadStack&, JsonConverter ) at System.Text.Json.JsonSerializer.ContinueDeserialize[LoginResult](ReadBufferState&, JsonReaderState&, ReadStack&, JsonConverter, JsonSerializerOptions ) at System.Text.Json.JsonSerializer.d__65 1[[MDashWeb.Shared.Authorization.LoginResult, MDashWeb.Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].MoveNext() at System.Net.Http.Json.HttpContentJsonExtensions.<ReadFromJsonAsyncCore>d__4 1[[ MDashWeb.Shared.Authorization.LoginResult, MDashWeb.Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].MoveNext() 在 MDashWeb.Client.Services.Implementations.AuthorizeApi.Login(LoginModel loginModel) 在 MDashWeb。 Client.Pages.Authentication.Login.OnSubmit() 在 Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(任务任务)在 AntDesign.Form`1.d__128[[MDashWeb.Shared.Authorization.LoginModel, MDashWeb .Shared,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null]].MoveNext() 在 Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(任务任务)在 Microsoft.AspNetCore.Components.Forms.EditForm.HandleSubmitAsync()
在 Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(任务任务) 在 Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(任务,ComponentState)

我想知道这是不是类似的东西?: https://github.com/do.net/as.netcore/issues/29162


我在 CLient 项目中有 AuthorizeApi.cs,内容如下:

public async Task<RegisterResult> Register(RegisterModel registerModel)
{
  HttpResponseMessage response = await this.HttpClient.PostAsJsonAsync("api/Authorize/Register", registerModel);
  return await response.Content.ReadFromJsonAsync<RegisterResult>();
}

然后在服务器项目中,我有 Controller,代码如下:

[Route("api/[controller]/[action]")]
[ApiController]
public class AuthorizeController : ControllerBase

...

[HttpPost]
public async Task<IActionResult> Register([FromBody] RegisterModel model)
{
  ApplicationUser newUser = new ApplicationUser
  {
    UserName = model.Email,
    Email = model.Email,
    FirstName = model.FirstName,
    LastName = model.LastName,
  };

...

感谢Henk Holterman为 OP 指明了正确的方向。 将您宝贵的讨论作为答案发布,以帮助其他社区成员。

很高兴用户LG3通过转到日志 Stream 解决了他的问题,该日志显示了所有异常及其详细信息,表明您指向了不正确的文件路径并提及参考提供了此类错误的解决方案,并且作为Henk Holterman 用户以另一种方式建议从api/Authorize/Register调用的浏览器开发工具中检查确切的 URL

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM