Reputation: 801
I have an ASP.NET MVC project that encompasses the bulk of a web application as well as a web API that is consumed by client-side code. The API component is beginning to sprawl and I would like to separate the API and MVC app into two projects. (MyAppMVC and MyAppAPI). However I am unsure how to get the MVC app to play nicely with the API when they are not part of one project.
Right now I have an API controller MyAPIController
that answers POST and GET requests sent from client-side JavaScript to /api/MyAPI
. When both the JS and API live under the same project, I run the solution from Visual Studio and hit the API with no issues.
However, when they run in different projects, this no longer cooperates. To avoid violating cross-domain security, the JS must be hitting the API within the same domain / port. However Visual Studio runs my API and MVC application on different ports, and attempting to hit the API from the Javascript file living inside the MVC file causes same-origin errors.
Is there any way to have these two different but complementary projects running on the same domain / port? For organization and modularity I would really like to be able to separate my web application into multiple projects rather than lumping them into a single massive one.
Upvotes: 4
Views: 5756
Reputation: 361
Go to Project Properties in Visual Studio and then go to Web, under servers change the project url to a different port number and Create Virtual Directory.
Upvotes: 1
Reputation: 801
So my fix, without using CORS ended up being to just give up on using IISExpress and create two separate applications under Local IIS, each with their own path, but both living under default web site
(localhost) at port 80. That way I was able to reach http://localhost/MyAppUI
and then make calls from MyAppUI
to http://localhost/MyAppAPI/api
without dealing with any cross-domain issues.
Upvotes: 1
Reputation: 535
I can suggest this way
Enable cross domain request in API
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="access-control-allow-headers" value="content-type" />
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
<system.webServer>
From that moment your projects will have
If you will do it, validate that you can brows to the projects before to run debug. You may have some problems like "access to the path denied" and so on.
Upvotes: 1