Section1> Blazor webassembly consuming API (Using DotNet 8.0 versions for all)
Create Blazor Wasm with No Authentication Create Web Api with No Authentication
![image](https://private-user-images.githubusercontent.com/3640748/290970473-e36ee6b9-e3d6-4fc1-ac3b-389422c92996.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjEzMjkxNjMsIm5iZiI6MTcyMTMyODg2MywicGF0aCI6Ii8zNjQwNzQ4LzI5MDk3MDQ3My1lMzZlZTZiOS1lM2Q2LTRmYzEtYWMzYi0zODk0MjJjOTI5OTYucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDcxOCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA3MThUMTg1NDIzWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9ODI1MjcwNTM3MWFlMTZhMmZhYjlhMDFlZjY1ODVlYzJhMzg1MWVjOWVlYzdmZjU1YmUwZTRiNDAxY2MwZGNiMSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.-Ersu0ArxQY6EMJVLuGPAbBrti_sp0Mrn3Mev9lUjZA)
ClientSide(wasm) No specific change required(unless credentials)
On Api Side, By default its like below
Then Try Consuming API by calling at clientside as below,
@inject HttpClient Http
....
protected override async Task OnInitializedAsync() {
var items = await Http.GetFromJsonAsync<WeatherForecast[]>("https://localhost:7298/WeatherForecast"); if (items != null) forecasts = items;//await Http.GetFromJsonAsync<WeatherForecast[]>("sample-data/weather.json");
}
APi URL can be taken from running instance on browser or cmd prompt
Http or Https both as per the needs,
https://localhost:7298/WeatherForecast
or
http://localhost:5161/WeatherForecast
Then CORS Error appears as like below,
![image](https://private-user-images.githubusercontent.com/3640748/290971764-8230005d-efc6-4563-b935-3e30fcd578c8.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjEzMjkxNjMsIm5iZiI6MTcyMTMyODg2MywicGF0aCI6Ii8zNjQwNzQ4LzI5MDk3MTc2NC04MjMwMDA1ZC1lZmM2LTQ1NjMtYjkzNS0zZTMwZmNkNTc4YzgucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDcxOCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA3MThUMTg1NDIzWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9ZDc4ZWQzZWY0MDI5M2EyNGQwNzQ2NjA1NWQ5MTQzNDU3MGVlOWJjOTUyMjAzOWRlMTcwOTg1NWIwZWQwYWU1NyZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.UkziScyt-4LML5ppOBxPBz4J3kk0EjccBFYinZq8hao)
Then Changes required are as below ,
//below required for localhost working builder.Services.AddCors(options => { options.AddDefaultPolicy(builder => { builder.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader(); }); }); //above required for localhost working builder.Services.AddControllers();
app.UseCors(); // required for localhost
app.UseHttpsRedirection();
It becomes as below,
Configure Startup Projects both Client Wasm & Api as running( Right click on Solution Explorer Solution name & choose )
Then
Then Run & enjoy further as per the need
Continuation of this thread are as below, https://stackoverflow.com/questions/68585377/access-to-xmlhttprequest-at-https-login-microsoftonline-com-has-been-blocked-b
https://www.freecodecamp.org/news/how-to-implement-azure-serverless-with-blazor-web-assembly/
Section2> Blazor webassembly consuming Azure Functions
Add Az functions project to solution
Then it becomes like this,
To enable add CORS like below(also can mention mention website name to allow instead of * ALL)
![image](https://private-user-images.githubusercontent.com/3640748/290976109-6ce51e29-e173-4a5a-bad6-484e2e564497.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjEzMjkxNjMsIm5iZiI6MTcyMTMyODg2MywicGF0aCI6Ii8zNjQwNzQ4LzI5MDk3NjEwOS02Y2U1MWUyOS1lMTczLTRhNWEtYmFkNi00ODRlMmU1NjQ0OTcucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDcxOCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA3MThUMTg1NDIzWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9NGZhOGNjYzZkZjlhYTg2NGQ1MWY3MGI5YWNjY2UzMTEzZGJlODVkMmEyYWQ3NjVlYjMwMWUxNWIzMDY4NzU0ZiZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.mSP57Tw_k4UZXDlkyeBvQq3v2D8t3blkET2swySv8-8)
Get the az function local URL from cmd prompt & test the same on browser for quick check
Then at clientside change consumption as below
Note:
1>For working on local environment wrt Azure functions,it needs some extra permission to run. Usually in many corporate provided systems its not allowed to run since its not trucsted properly.So try for suitable respective solution .
2> For working locally,its better to maintain configurations is separate config files & not to check in . ANd always preferred to not check in this file. Still if you want to check in change on gitignore to include.
Like Developement.settings.json or local.settings.json
3>For enabling CORS on local make it like this
4> To fix CORS issue 2 apporoches usually, Temporary Front-End solution so you can test if your API integration is working: Click on windows -> type run and hit enter -> in the command window copy: chrome.exe --user-data-dir="C://Chrome dev session" --disable-web-security This will open a new "Chrome" window where you can work easily. This is a temporary solution. Every time you will have to work with this chrome window.
Permanent solution: In the backend code, the developer needs to add an annotation @Crossorigin right above the CRUD api call method.