Add Access Control Origin Response Header

Leonardo Santos 1 Reputation point
2021-07-13T18:43:58.733+00:00

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

114326-1.png

Following this example:

https://learn.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?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,402 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,650 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Leonardo Santos 1 Reputation point
    2021-07-14T14:18:19.32+00:00

    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

    0 comments No comments