How to solve the “System.InvalidOperationException: The given header was not found.” exception in Blazor WASM Client

huangapple go评论88阅读模式
英文:

How to solve the “System.InvalidOperationException: The given header was not found.” exception in Blazor WASM Client

问题

In my Blazor WASM client project, I need to call an API and put the value of "X-Pagination" in its header, unfortunately, when I call this API I get this exception in the browser console:

    WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: The given header was not found.
   System.InvalidOperationException: The given header was not found.
   at System.Net.Http.Headers.HttpHeaders.GetValues(HeaderDescriptor descriptor)
   at System.Net.Http.Headers.HttpHeaders.GetValues(String name)

My API:

    [HttpPost]
    public async Task<ApiResult<PagedList<ReceiptDto>>> GetAll(Pagable pagable, CancellationToken cancellationToken)
    {
        Guid? userId = User.Identity.GetGuidUserId();

        GetReceiptsQuery query = new(pagable, (Guid)userId);
        var result = await _mediator.Send(query, cancellationToken);

        Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(result.Data.MetaData));

        return result;
    }

Also I added .AllowAnyHeader() method to the CORS policy API in the startup class:

   services.AddCors(
            options =>
            {
                options.AddDefaultPolicy(builder =>
                {
                    builder.WithOrigins(
                        "https://localhost:44366/",
                        "http://localhost:44366/")
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowAnyOrigin();
                });
            })

In my method in wasm client, I call my httpclient and get the values of header:

    public async Task<PagingResponse<ReceiptDto>> GetReceipts(Pagable pagable)
    {
        HttpResponseMessage response = await _client.PostAsJsonAsync("v1/Users/Receipts/GetAll", pagable);

        if (!response.IsSuccessStatusCode)
        {
            throw new ApplicationException(response.Content.ToString());
        }

        var result = await response.Content.ReadAsAsync<ApiResult<List<ReceiptDto>>>();

        if (!result.IsSuccess)
        {
            throw new ApplicationException(result.Message);
        }

        PagingResponse<ReceiptDto> pagingResponse = new()
        {
            Items = result.Data,
            MetaData = JsonSerializer.Deserialize<MetaData>(response.Headers.GetValues("X-Pagination").First(), _options)
        };

        return pagingResponse;
    }

Whereas when I call this API in Postman or Swagger there is no problem:

How to solve the “System.InvalidOperationException: The given header was not found.” exception in Blazor WASM Client

but as I said at the beginning of my question, when I call this API in the Blazor WASM client, I get the above error.

I encounter an error while calling API, I shared it to solve it.

英文:

In my Blazor WASM client project, I need to call an API and put the value of "X-Pagination" in its header, unfortunately, when I call this API I get this exception in the browser console:

    WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: The given header was not found.
   System.InvalidOperationException: The given header was not found.
   at System.Net.Http.Headers.HttpHeaders.GetValues(HeaderDescriptor descriptor)
   at System.Net.Http.Headers.HttpHeaders.GetValues(String name)

My API:

    [HttpPost]
    public async Task&lt;ApiResult&lt;PagedList&lt;ReceiptDto&gt;&gt;&gt; GetAll(Pagable pagable, CancellationToken cancellationToken)
    {
        Guid? userId = User.Identity.GetGuidUserId();

        GetReceiptsQuery query = new(pagable, (Guid)userId);
        var result = await _mediator.Send(query, cancellationToken);

        Response.Headers.Add(&quot;X-Pagination&quot;, JsonConvert.SerializeObject(result.Data.MetaData));

        return result;
    }

Also I added .AllowAnyHeader() method to the CORS policy API in the startup class:

   services.AddCors(
            options =&gt;
            {
                options.AddDefaultPolicy(builder =&gt;
                {
                    builder.WithOrigins(
                        &quot;https://localhost:44366/&quot;,
                        &quot;http://localhost:44366/&quot;)
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowAnyOrigin();
                });
            })

In my method in wasm client, I call my httpclient and get the values of header :

    public async Task&lt;PagingResponse&lt;ReceiptDto&gt;&gt; GetReceipts(Pagable pagable)
    {
        HttpResponseMessage response = await _client.PostAsJsonAsync(&quot;v1/Users/Receipts/GetAll&quot;, pagable);

        if (!response.IsSuccessStatusCode)
        {
            throw new ApplicationException(response.Content.ToString());
        }

        var result = await response.Content.ReadAsAsync&lt;ApiResult&lt;List&lt;ReceiptDto&gt;&gt;&gt;();

        if (!result.IsSuccess)
        {
            throw new ApplicationException(result.Message);
        }

        PagingResponse&lt;ReceiptDto&gt; pagingResponse = new()
        {
            Items = result.Data,
            MetaData = JsonSerializer.Deserialize&lt;MetaData&gt;(response.Headers.GetValues(&quot;X- Pagination&quot;).First(), _options)
        };

        return pagingResponse;
    }

Whereas when I call this API in Postman or Swagger there is no problem :
How to solve the “System.InvalidOperationException: The given header was not found.” exception in Blazor WASM Client

but as I said at the beginning of my question, when I call this API in the Blazor WASM client, I get the above error.

I encounter an error while calling API, I shared it to solve it.

答案1

得分: 2

我最终意识到API的CORS策略应该指定哪些标头被暴露,因此我解决了异常并在API启动类中更改了CORS设置如下:

services.AddCors(
    options =>
    {
        options.AddDefaultPolicy(builder =>
        {
            builder.WithOrigins(
                "https://localhost:44366/",
                "http://localhost:44366/")
            .AllowAnyMethod()
            .AllowAnyHeader()
            .AllowAnyOrigin()
            .WithExposedHeaders("X-Pagination");  // 这一行很重要
        });
    })
英文:

I finally realized that the API CORS policy should specify what headers are exposed, so I resolved the exception and changed the CORS settings in the API startup class as below:

services.AddCors(
            options =&gt;
            {
                options.AddDefaultPolicy(builder =&gt;
                {
                    builder.WithOrigins(
                        &quot;https://localhost:44366/&quot;,
                        &quot;http://localhost:44366/&quot;)
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowAnyOrigin()
                    .WithExposedHeaders(&quot;X-Pagination&quot;);  // this line is important
                });
            })

huangapple
  • 本文由 发表于 2023年5月14日 14:38:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76246170.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定