0

I have an old project running happily in VS2019 - a RESTful web service and some javascript code to access the service. It was written a while ago, using a much older version of VS.

I can do POST requests with JSON data in the development environment - when the project starts running, I have set it to open index.html and start the web service. However if I just start the service (by selecting 'don't open a page, wait for a request...') and then access the service from outside of VS, I am getting a CORS error - presumably because the client and server are now running on different ports.

Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

I have read a lot about CORS (there are so many posts on here!) and as a result have included the following in web.config:

<system.webServer>
    <httpProtocol>
      <customHeaders>
        <clear />
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers" />
        <add name="Access-Control-Allow-Methods" value="GET, PUT, POST,OTIONS" />
      </customHeaders>
    </httpProtocol>
 </system.webServer>

I have also found this which explains how to set up CORS on ASP.NET but it doesn't seem to apply to my situation (I have installed the suggested CORS package with NuGet, but I don't have any c# configuration code in my source - the only config is `web.config').

So, how can I allow access to my service (in the dev environment) from outside of VS? I need to do this because I want to completely rewrite the client code using VSCode and Vue.

I could start a new web app project in VS2022, using the latest technologies, and copy my server code accross. I am prepared to do this if it will solve the CORS problem - but is there an easier way?

[edit] In response to @quentin's comments:

  • I mentioned VS2019 because it seemlessly integrates the client JScript with the server API without CORS errors
  • Am I using ASP.NET? I think so, although there's not much mention of it in the project. The debugger is set to ASP.NET, and the code works well on the remote (production) server which is ASP.NET
  • Server language is c#, .NET 4.5.2, running on IIS Express
quilkin
  • 874
  • 11
  • 31
  • "in VS2019" — Your choice of IDE shouldn't be relevant. – Quentin Aug 08 '23 at 09:18
  • "how to set up CORS on ASP.NET" — Are you using ASP.NET? – Quentin Aug 08 '23 at 09:19
  • 1
    I think you need to provide more information about the server side programming languages, frameworks, and their versions. (And apply suitable tags, beyond just [tag:cors], to the question). – Quentin Aug 08 '23 at 09:20
  • Found the solution [here](https://stackoverflow.com/questions/14047754/) (the accepted `global.asax` answer) – quilkin Aug 09 '23 at 07:16

0 Answers0