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();
});
}
3
Answers
Thanks to Serge .
According to Serge helps ,services.AddControllers() are duplicated . I remove one of them and project works properly.
you are using a wrong syntax, try this
If the request exceed the
maxAllowedContentLength
limitation, which would cause the issue even though we have enabled CORS.Please check if the issue happens while the browser client upload large file(s). And you can try to increase the value of
maxAllowedContentLength
on your web server based on your actual scenario then check if it can help fix the issue.