Reputation: 463
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");
Upvotes: 0
Views: 3049
Reputation: 1
Make sure your url must be complete for example https://www.xxxxxx.com.... or http://www.xxxxxx.com....
For published API with paid ssl certificate to get data from API on client side
Example:
private readonly HttpClient httpClient;
public TblMenuSER(HttpClient httpClient)
{
this.httpClient = httpClient;
}
public async Task<IEnumerable<TblMenus>>Getoutput()
{
return await this.httpClient.GetFromJsonAsync<TblMenus[]>("https://xxxxx.com/BLZ/api/GETData/x,x,x,x");
}
Upvotes: 0
Reputation: 21
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.
Upvotes: 2