sir_thursday sir_thursday - 29 days ago 10
ASP.NET (C#) Question

Sending JSON between localhost ports

I had an ASP.NET MVC app running on port 8799 that posted JSON data via an

XMLHttpRequest
to an MVC controller handler. This pipeline was working perfectly fine.

I recently spun up a second app, this time an Express app running on port 8080, that again posts JSON in the same way (same exact code, using again an
XMLHttpRequest
), to the same MVC controller running on port 8799. This time, however, it appears as though while the request is received by the controller, there is no data on it.

I'm wondering if this is because I am making a cross-origin request between these two ports. Going off of this hypothesis, I added the following custom headers to my
web.config
file:

<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://localhost" />
<add name="Access-Control-Allow-Headers" value="X-AspNet-Version,X-Powered-By,Date,Server,Accept,Accept-Encoding,Accept-Language,Cache-Control,Connection,Content-Length,Content-Type,Host,Origin,Pragma,Referer,User-Agent" />
<add name="Access-Control-Allow-Methods" value="GET, PUT, POST, DELETE, OPTIONS" />
<add name="Access-Control-Max-Age" value="1000" />
</customHeaders>


This does not seem to solve the issue. How can I debug this problem, I don't seem to be throwing any particularly useful exceptions?

Answer Source

The issue did turn out to be CORS. For whatever reason the web.config file was not being used by my .NET application. Thus, I setup CORS setting in my Startup.cs file instead, using services.AddCors() in my Configure() call.