0

I have been trying to call a separate asp.net core web api from with in a Blazor client side web assembly. I used the templates web API template with this controller. I have CORS set up in the startup.cs. I am using the standard HTTP client with in the Blazor Client. Running locally I can set a break point on the API code and see it get hit. But the client is throwing an exception: Type Error

Any help would be greatly appreciated.

David

[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
    private static readonly string[] Summaries = new[]
    {
        "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    };

    private readonly ILogger<WeatherForecastController> _logger;

    public WeatherForecastController(ILogger<WeatherForecastController> logger)
    {
        _logger = logger;
    }

    [HttpGet]
    public IEnumerable<WeatherForecast> Get()
    {
        var rng = new Random();
        return Enumerable.Range(1, 5).Select(index => new WeatherForecast
        {
            Date = DateTime.Now.AddDays(index),
            TemperatureC = rng.Next(-20, 55),
            Summary = Summaries[rng.Next(Summaries.Length)]
        })
        .ToArray();
    }
}

Startup.cs
     services.AddCors(options =>
            {
                options.AddPolicy(name: MyAllowSpecificOrigins,
                                  builder =>
                                  {
                                      builder.WithOrigins("http://example.com",
                                                          "http://www.contoso.com")
                                                          .AllowAnyOrigin()
                                                          .AllowAnyMethod()
                                                          .AllowAnyHeader();
                                  });
            });

Blazor Client
var response = await Http.GetFromJsonAsync<WeatherForecast[]>("WeatherForecast");
David23g
  • 213
  • 1
  • 2
  • 6

1 Answers1

2

I got the same kind of error and upon using the developer tool console , looks like the mixed content is not allowed (both https and http).Mixed Content: The page at 'https://' was loaded over HTTPS, but requested an insecure resource 'http://'. This request has been blocked; the content must be served over HTTPS. Check whether this is the case with your pages too.