Add cors policy
This commit is contained in:
@@ -4,11 +4,17 @@
|
|||||||
<TargetFramework>net9.0</TargetFramework>
|
<TargetFramework>net9.0</TargetFramework>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<UserSecretsId>5637e3c4-2341-4bdb-85ec-c75faeee9847</UserSecretsId>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="9.0.0" />
|
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="9.0.2" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="9.0.0" PrivateAssets="all" />
|
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="9.0.2" PrivateAssets="all" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.2" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\..\GameIdeas.Resources\GameIdeas.Resources.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
@inherits LayoutComponentBase
|
@inherits LayoutComponentBase
|
||||||
|
|
||||||
<div class="page">
|
<div class="page">
|
||||||
<div class="sidebar">
|
<div class="sidebar">
|
||||||
<NavMenu />
|
<NavMenu />
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
using Microsoft.AspNetCore.Components;
|
||||||
|
using System.Net.Http.Json;
|
||||||
|
using GameIdeas.Resources;
|
||||||
|
|
||||||
|
namespace GameIdeas.BlazorApp.Layout;
|
||||||
|
|
||||||
|
public partial class MainLayout(
|
||||||
|
IHttpClientFactory HttpClientFactory,
|
||||||
|
TranslationService TranslationService,
|
||||||
|
Translations Translations) : LayoutComponentBase
|
||||||
|
{
|
||||||
|
protected override async Task OnInitializedAsync()
|
||||||
|
{
|
||||||
|
var client = HttpClientFactory.CreateClient("GameIdeas.WebAPI");
|
||||||
|
var response = await client.GetAsync("api/Translations");
|
||||||
|
var dictionary = await response.Content.ReadFromJsonAsync<Dictionary<string, string>>();
|
||||||
|
if (dictionary != null)
|
||||||
|
{
|
||||||
|
TranslationService.Initialize(dictionary);
|
||||||
|
ResourcesKey.Initialize(Translations);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using GameIdeas.WebApp;
|
using GameIdeas.BlazorApp;
|
||||||
|
using GameIdeas.Resources;
|
||||||
using Microsoft.AspNetCore.Components.Web;
|
using Microsoft.AspNetCore.Components.Web;
|
||||||
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
|
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
|
||||||
|
|
||||||
@@ -6,6 +7,20 @@ var builder = WebAssemblyHostBuilder.CreateDefault(args);
|
|||||||
builder.RootComponents.Add<App>("#app");
|
builder.RootComponents.Add<App>("#app");
|
||||||
builder.RootComponents.Add<HeadOutlet>("head::after");
|
builder.RootComponents.Add<HeadOutlet>("head::after");
|
||||||
|
|
||||||
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
|
UriBuilder uriBuilder = new(builder.HostEnvironment.BaseAddress)
|
||||||
|
{
|
||||||
|
Port = 8000
|
||||||
|
};
|
||||||
|
|
||||||
await builder.Build().RunAsync();
|
builder.Services.AddHttpClient(
|
||||||
|
"GameIdeas.WebAPI",
|
||||||
|
client =>
|
||||||
|
{
|
||||||
|
client.BaseAddress = uriBuilder.Uri;
|
||||||
|
client.Timeout = TimeSpan.FromMinutes(3);
|
||||||
|
});
|
||||||
|
|
||||||
|
builder.Services.AddSingleton<TranslationService>();
|
||||||
|
builder.Services.AddSingleton<Translations>();
|
||||||
|
|
||||||
|
await builder.Build().RunAsync();
|
||||||
@@ -6,5 +6,5 @@
|
|||||||
@using Microsoft.AspNetCore.Components.Web.Virtualization
|
@using Microsoft.AspNetCore.Components.Web.Virtualization
|
||||||
@using Microsoft.AspNetCore.Components.WebAssembly.Http
|
@using Microsoft.AspNetCore.Components.WebAssembly.Http
|
||||||
@using Microsoft.JSInterop
|
@using Microsoft.JSInterop
|
||||||
@using GameIdeas.WebApp
|
@using GameIdeas.BlazorApp
|
||||||
@using GameIdeas.WebApp.Layout
|
@using GameIdeas.BlazorApp.Layout
|
||||||
|
|||||||
@@ -1,27 +0,0 @@
|
|||||||
[
|
|
||||||
{
|
|
||||||
"date": "2022-01-06",
|
|
||||||
"temperatureC": 1,
|
|
||||||
"summary": "Freezing"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"date": "2022-01-07",
|
|
||||||
"temperatureC": 14,
|
|
||||||
"summary": "Bracing"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"date": "2022-01-08",
|
|
||||||
"temperatureC": -13,
|
|
||||||
"summary": "Freezing"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"date": "2022-01-09",
|
|
||||||
"temperatureC": -16,
|
|
||||||
"summary": "Balmy"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"date": "2022-01-10",
|
|
||||||
"temperatureC": -2,
|
|
||||||
"summary": "Chilly"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
@@ -9,6 +9,11 @@ builder.Services.AddControllers();
|
|||||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
||||||
builder.Services.AddOpenApi();
|
builder.Services.AddOpenApi();
|
||||||
|
|
||||||
|
builder.Services.AddCors(option => option.AddDefaultPolicy(policy =>
|
||||||
|
policy.WithOrigins("http://localhost:5172")
|
||||||
|
.AllowAnyHeader()
|
||||||
|
.WithMethods("GET", "POST")));
|
||||||
|
|
||||||
builder.Services.AddSingleton<TranslationService>();
|
builder.Services.AddSingleton<TranslationService>();
|
||||||
builder.Services.AddSingleton<Translations>();
|
builder.Services.AddSingleton<Translations>();
|
||||||
|
|
||||||
@@ -32,9 +37,9 @@ foreach (var file in translationFiles)
|
|||||||
}
|
}
|
||||||
|
|
||||||
app.Services.GetRequiredService<TranslationService>().Initialize(dictionary);
|
app.Services.GetRequiredService<TranslationService>().Initialize(dictionary);
|
||||||
var resourcesKey = app.Services.GetRequiredService<Translations>();
|
ResourcesKey.Initialize(app.Services.GetRequiredService<Translations>());
|
||||||
ResourcesKey.Initialize(resourcesKey);
|
|
||||||
|
|
||||||
|
app.UseCors();
|
||||||
app.UseAuthorization();
|
app.UseAuthorization();
|
||||||
|
|
||||||
app.MapControllers();
|
app.MapControllers();
|
||||||
|
|||||||
Reference in New Issue
Block a user