【问题标题】:Unhandled exception rendering component: '<' is an invalid start of a value for Blazor WASM未处理的异常呈现组件:“<”是 Blazor WASM 值的无效开始
【发布时间】:2021-07-21 03:01:27
【问题描述】:

我有一个允许匿名的 API 端点。我已经在 Postman 中确认它适用于匿名和经过身份验证的用户并返回正确的 JSON。

由于可以匿名调用端点,所以在我的 Blazor WASM http Service 中,我构造了以下内容,一个不带令牌发送的请求,一个带令牌发送的请求。

private readonly HttpClient httpClient;
private readonly IHttpClientFactory httpClientFactory;
private readonly HttpClient anonHttp;

public CommunityHttpService(HttpClient httpClient, IHttpClientFactory httpClientFactory)
{
   if (httpClient == null) throw new ArgumentNullException("Http is null.");
   this.httpClient = httpClient;
   this.httpClientFactory = httpClientFactory;
   anonHttp = httpClientFactory.CreateClient("AnonAPI");
}

public async Task<HttpResponseMessage> GetCommunity(Guid communityId, bool userIsAuthenticated)
{
    if (!userIsAuthenticated)
    {
       // make anonymous api call without bearer token to avoid error
       return await anonHttp.GetAsync($"api/communities/GetCommunity/" + communityId);
    }
    else
    {
       return await httpClient.GetAsync($"api/communities/GetCommunity/" + communityId);
     }
}

我在我的组件中声明了社区。​​p>

private CommunityDto Community { get; set; } = new CommunityDto();

在我的组件的 OnInitializedAsync 方法中,我有以下内容:

if (!string.IsNullOrEmpty(CommunityId.ToString()))
{
    var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
    var user = authState.User;
    HttpResponseMessage apiResponse;
    if (user.Identity.IsAuthenticated)
    {
       apiResponse = await CommunityService.GetCommunity(CommunityId, true);
    }
    else
    {
      apiResponse = await CommunityService.GetCommunity(CommunityId, false);
    }

    if (apiResponse.IsSuccessStatusCode)
    {
        Community = await apiResponse.Content.ReadFromJsonAsync<CommunityDto>();
    }
}

所有的html是:

<span>@CommunityId</span>

设置断点似乎没用,但我不知道是什么原因导致控制台出现此错误。有什么想法吗?

【问题讨论】:

    标签: blazor blazor-webassembly


    【解决方案1】:

    错误说&lt;html&gt;不是有效的JSon。

    表示"api/communities/GetCommunity/" 不正确。或 Program.cs 中配置的 BaseAddress。

    使用开发工具的“网络”选项卡查看正在使用的 URL,并将其与您在 PostMan 中使用的进行比较。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-21
      • 2020-11-04
      • 2020-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-04
      相关资源
      最近更新 更多