Asp.net 服务器应用程序在本地主机上工作,但在 Azure 中不工作(未处理的异常呈现组件:ExpectedJsonTokens)
Asp.net Server application is working on localhost, but not in Azure (Unhandled exception rendering component: ExpectedJsonTokens)
我已经创建了一个 Web 应用程序,这是我第一次将它带入 Azure。我已按照所有步骤操作并获得了 SQL 以及所有连接字符串等。我可以从网址访问我的应用程序。但是,一旦我尝试从 Web 登录或注册,我就会遇到异常。
我做了什么:
在 Visual Studio 的解决方案中打开 appsettings.json
并将连接字符串添加到 Azure SQL。然后 运行 我的应用程序在我的计算机上处于调试模式并尝试注册一个新用户 => 一切正常!因此,如果我从我的计算机 (https://localhost:7113/login) 访问 Azure SQL,一切正常,我可以读取和写入数据库。如果我从 https://xxx.azurewebsites.net/login 做同样的事情,我会收到下面提到的错误。知道为什么会这样以及如何解决吗?
跟设置apAPI路由有关系吗?
我的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
}
}
}
我正在以这种方式构建解决方案模型:客户端、服务器、共享
crit:
Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: ExpectedJsonTokens Path: $ | LineNumber: 0 | BytePositionInLine: 0.
System.Text.Json.JsonException: ExpectedJsonTokens Path: $ |
LineNumber: 0 | BytePositionInLine: 0. --->
System.Text.Json.JsonReaderException: ExpectedJsonTokens LineNumber: 0
| BytePositionInLine: 0. at
System.Text.Json.ThrowHelper.ThrowJsonReaderException(Utf8JsonReader&
, ExceptionResource , Byte , ReadOnlySpan1 ) at System.Text.Json.Utf8JsonReader.Read() at 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& ) Exception_EndOfInnerExceptionStack at
System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& ,
JsonReaderException ) at
System.Text.Json.Serialization.JsonConverter1[[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__651[[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() at
MDashWeb.Client.Services.Implementations.AuthorizeApi.Login(LoginModel
loginModel) at
MDashWeb.Client.Pages.Authentication.Login.OnSubmit() at
Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task
task) at
AntDesign.Form`1.d__128[[MDashWeb.Shared.Authorization.LoginModel,
MDashWeb.Shared, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null]].MoveNext() at
Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task
task) at
Microsoft.AspNetCore.Components.Forms.EditForm.HandleSubmitAsync()
at
Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task
task) at
Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task
, ComponentState )
我想知道这是不是类似的东西?:https://github.com/dotnet/aspnetcore/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>();
}
然后在服务器项目中我有控制器,代码如下:
[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 for resolved his issue by going to the Log Stream which shows all the exceptions and their details which shows you that you're pointing to an incorrect file path and mentioning the reference 给出了此类错误的解决方案,正如 Henk Holterman 用户所建议的那样,以另一种方式检查 确切 URL 来自 api/Authorize/Register 调用的浏览器开发工具。
我已经创建了一个 Web 应用程序,这是我第一次将它带入 Azure。我已按照所有步骤操作并获得了 SQL 以及所有连接字符串等。我可以从网址访问我的应用程序。但是,一旦我尝试从 Web 登录或注册,我就会遇到异常。
我做了什么:
在 Visual Studio 的解决方案中打开 appsettings.json
并将连接字符串添加到 Azure SQL。然后 运行 我的应用程序在我的计算机上处于调试模式并尝试注册一个新用户 => 一切正常!因此,如果我从我的计算机 (https://localhost:7113/login) 访问 Azure SQL,一切正常,我可以读取和写入数据库。如果我从 https://xxx.azurewebsites.net/login 做同样的事情,我会收到下面提到的错误。知道为什么会这样以及如何解决吗?
跟设置apAPI路由有关系吗?
我的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
}
}
}
我正在以这种方式构建解决方案模型:客户端、服务器、共享
crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] Unhandled exception rendering component: ExpectedJsonTokens Path: $ | LineNumber: 0 | BytePositionInLine: 0. System.Text.Json.JsonException: ExpectedJsonTokens Path: $ | LineNumber: 0 | BytePositionInLine: 0. ---> System.Text.Json.JsonReaderException: ExpectedJsonTokens LineNumber: 0 | BytePositionInLine: 0. at System.Text.Json.ThrowHelper.ThrowJsonReaderException(Utf8JsonReader& , ExceptionResource , Byte , ReadOnlySpan
1 ) at System.Text.Json.Utf8JsonReader.Read() at 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& ) Exception_EndOfInnerExceptionStack at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& , JsonReaderException ) at System.Text.Json.Serialization.JsonConverter1[[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__651[[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() at MDashWeb.Client.Services.Implementations.AuthorizeApi.Login(LoginModel loginModel) at MDashWeb.Client.Pages.Authentication.Login.OnSubmit() at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task) at AntDesign.Form`1.d__128[[MDashWeb.Shared.Authorization.LoginModel, MDashWeb.Shared, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].MoveNext() at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task) at Microsoft.AspNetCore.Components.Forms.EditForm.HandleSubmitAsync()
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task) at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task , ComponentState )
我想知道这是不是类似的东西?:https://github.com/dotnet/aspnetcore/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>();
}
然后在服务器项目中我有控制器,代码如下:
[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 for resolved his issue by going to the Log Stream which shows all the exceptions and their details which shows you that you're pointing to an incorrect file path and mentioning the reference 给出了此类错误的解决方案,正如 Henk Holterman 用户所建议的那样,以另一种方式检查 确切 URL 来自 api/Authorize/Register 调用的浏览器开发工具。