question

LeonardoSantos-9663 avatar image
0 Votes"
LeonardoSantos-9663 asked LeonardoSantos-9663 edited

Add Access Control Origin Response Header

In my ASP.NET CORE MVC, there is a dist folder in wwwroot.

114326-1.png

Following this example:

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-5.0

I configured the startup class to add the Access Control Allow Origin header:



    public class Startup
     {
         public Startup(IConfiguration configuration)
         {
             Configuration = configuration;
         }
    
         public IConfiguration Configuration { get; }
    
         public void ConfigureServices(IServiceCollection services)
         {
             services.AddRazorPages();
         }
    
            
         public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
         {
             if (env.IsDevelopment())
             {
                 app.UseDeveloperExceptionPage();
             }
             else
             {
                 app.UseExceptionHandler("/Error");
                    
                 app.UseHsts();
             }
    
             app.UseHttpsRedirection();
    
             app.UseStaticFiles(new StaticFileOptions
             {
                 OnPrepareResponse = ctx =>
                 {
                     // using Microsoft.AspNetCore.Http;
                     ctx.Context.Response.Headers.Append(
                          "Access-Control-Allow-Origin", "*");
             }
             });
    
             app.UseRouting();
    
             app.UseEndpoints(endpoints =>
             {
                 endpoints.MapRazorPages();
             });
         }
     }

On the other hand, the Header wasn't added (functions.json):

114265-2.png

am I missing something?










dotnet-csharpdotnet-aspnet-core-mvc
1.png (10.6 KiB)
2.png (10.3 KiB)
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

What exactly are you trying to do? Share static files with an application on another domain???

0 Votes 0 ·

I have a front-end application that generates the static files of the dist folder. So, I am trying to serve these archives using ..net Core as a back-end. On the other hand, the functions.json has been blocked by CORS: Access to XMLHttpRequest at 'https://www.xxxxxx.com.br/dist/functions.json' from origin 'https://excel.officeapps.live.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Then, I'm trying to set up an Access Control Allow Origin header for these archives.

0 Votes 0 ·
AgaveJoe avatar image AgaveJoe LeonardoSantos-9663 ·

I tested your configuration and it returns the expected header in the response.

 app.UseStaticFiles(new StaticFileOptions
 {
     OnPrepareResponse = ctx =>
     {
         ctx.Context.Response.Headers.Add("Access-Control-Allow-Origin", "*");     
     }
 });

Perhaps there is something else wrong.

114344-capture.png



0 Votes 0 ·
capture.png (15.2 KiB)

1 Answer

LeonardoSantos-9663 avatar image
0 Votes"
LeonardoSantos-9663 answered LeonardoSantos-9663 edited

I changed the code like that:

Before :

ctx.Context.Response.Headers.Append("Access-Control-Allow-Origin", "*");


now:

ctx.Context.Response.Headers.Add("Access-Control-Allow-Origin", "*");

I realized that the header was applied in js and lib folders. On the other hand, css and dist didn't receive the header. So, I renamed the dist folder and put it in the lib folder. It works!

Thank you AgaveJoe

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.