feat: add CORS for /api/blog controller

This commit is contained in:
Oliver Booth 2023-08-11 17:16:26 +01:00
parent 415726cdcd
commit 9d0e16abc1
Signed by: oliverbooth
GPG Key ID: 725DB725A0D9EE61
2 changed files with 8 additions and 0 deletions

View File

@ -1,4 +1,5 @@
using Humanizer;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using OliverBooth.Data.Blog;
using OliverBooth.Services;
@ -11,6 +12,7 @@ namespace OliverBooth.Controllers;
[ApiController]
[Route("api/blog")]
[Produces("application/json")]
[EnableCors("BlogApi")]
public sealed class BlogApiController : ControllerBase
{
private readonly BlogService _blogService;

View File

@ -34,6 +34,11 @@ builder.Services.AddDbContextFactory<WebContext>();
builder.Services.AddSingleton<BlogService>();
builder.Services.AddRazorPages().AddRazorRuntimeCompilation();
builder.Services.AddControllersWithViews();
builder.Services.AddCors(options => options.AddPolicy("BlogApi", policy => (builder.Environment.IsDevelopment()
? policy.AllowAnyOrigin()
: policy.WithOrigins("https://oliverbooth.dev"))
.AllowAnyMethod()
.AllowAnyHeader()));
builder.Services.AddRouting(options => options.LowercaseUrls = true);
builder.WebHost.UseKestrel(kestrel =>
@ -79,6 +84,7 @@ app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseCors("BlogApi");
app.MapControllers();
app.MapRazorPages();