.Net Core 3.1/.Net 5 CORS

Matt Perley
2 min readDec 22, 2020

--

Have you run into an xmlhttp error? Do you think it could be due to your .Net backend? It very well could be.

I was having some issues recently when trying to connect a web application to my backend, it kept giving me cors errors. So naturally I started looking around for a fix and it honestly seemed kind of pointless, no errors displayed in my .Net Core app and it only said xmlhttperror in my dart front end. I worked around this by using windows servers and setting up headers manually. This got annoying however, so I started playing and eventually found this bit of magic.

In your ConfigureServices function, simply add servces.AddCors();
Thats it.

Then…

We want to run the Cors builder inside the configure function. This will actually set up the headers and will work properly when making requests via dart/javascript/etc. Normally I work on desktop and mobile applications and this does not really seem to be much of an issue there. However in the world of the web, things are not always so simple I find.

I hope this was useful to someone out there. It was for me! Until next time, take care.

--

--

Matt Perley
Matt Perley

Written by Matt Perley

I could go on all day in here.

No responses yet