0
I’m having enough trouble solving a CORS error.
I am following the following steps:
- I added the CORS Nuget package to my project.
- I’m adding the code
config.EnableCors();
in my classstartUp
. - I added the attribute
[EnableCors]
to classController
.
even following these steps I keep getting error from CORS
Access to Xmlhttprequest at 'http://xxx.xxx.x.xxx/api/meuprojeto/Singup/' from origin 'https://xxx.xxx.x.xxx' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header contains Multiple values '*, *', but only one is allowed.
Startup Code:
public void Configuration(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
var formatters = config.Formatters;
formatters.Remove(formatters.XmlFormatter);
var jsonSettings = formatters.JsonFormatter.SerializerSettings;
jsonSettings.Formatting = Formatting.Indented;
jsonSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.MapHttpAttributeRoutes();
//WEB API configuration and services
config.EnableCors();
config.Routes.MapHttpRoute(
name: "DefaultRoute",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.UseWebApi(config);
}
My controller:
namespace OdyssAppws.ControllerOdyss
{
[EnableCors(origins: "https://xxx.xxx.x.xxx/", headers: "*", methods: "*")]
[RoutePrefix("api/meuprojeto")]
public class IdUserController : ApiController
{
Dude, I don’t think you need to put config.enablecors and app.usecors, just leave the app.usecors and try again
– Lucas Miranda
@Lucasmiranda continues pointing Cors error
– Gabriel Silva
Access to Xmlhttprequest at 'http://xxx.xxx.x.xxx/Singup/' from origin 'https://xxx.xxx.x.xxx' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested source.
– Gabriel Silva
Try
config.EnableCors(new EnableCorsAttribute(Properties.Settings.Default.Cors, "", ""))
and remove the attribute.– Barbetta
@Barbetta Hello, sorry, it was not very clear to me Remove that attribute ?
– Gabriel Silva
Remove the attribute
EnableCors
from above the controller– Barbetta
@Barbetta without success :/
– Gabriel Silva
The problem may be in the way I am making the request for my api ?
– Gabriel Silva