I have configured a Configure method in my Startup.cs file like this:
app.UseCors(option => option
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod() );
But when publishing a project and uploading files to Plesk, the HttpPost method won't work at all, and I get this error:
Cross-Origin Request Blocked: the same origin policy disallows reading the remote resource (Reason: CORS header ‘Access-Control-Allow-Origin’ missing). Status code: 500.
I have been set AllowAnyHeader() previously but it seems it commands does not work. Why - and how can I fix this?
Edit:
I have these snipped in the Web.config:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
<security>
<requestFiltering>
<verbs>
<remove verb="OPTIONS" />
</verbs>
</requestFiltering>
</security>
</system.webServer>
But whole of site wont run.
Edit:
I have used these snipped in the Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddControllers()
.AddJsonOptions(o => o.JsonSerializerOptions
.ReferenceHandler = ReferenceHandler.Preserve);
services.AddMemoryCache();
services.AddCors(opt => opt.AddPolicy("CorsPolicy", c =>
{
c.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
}));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseCors("CorsPolicy");
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}