Suddenly getting CORS errors in a .Net web application

stegriff

Ste Griffiths

Posted on July 16, 2021

Suddenly getting CORS errors in a .Net web application

A call from my SPA to the .Net backend, that had been working fine, started going red in my browser DevTools 😱:

POST + Preflight | (failed) net::ERR_CONNECTION_REFUSED
Enter fullscreen mode Exit fullscreen mode

I knew my CORS config hadn’t changed, and in fact the app is built with appBuilder.UseCors(CorsOptions.AllowAll);

While Ducking around for a solution, I caught sight of a little comment on an SO answer, which said “be aware that exceptions will CLEAN off any CORS headers set by the CORS middleware”.

I returned to VS, opened Exception settings, and turned on all CLR exceptions.

When I repeated the call into the API, whaddya know, AutoMapper was throwing a silent error.

After I fixed my mapping, the CORS error went away 😊

Happy hacking!

💖 💪 🙅 🚩
stegriff
Ste Griffiths

Posted on July 16, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related