UpQuark
UpQuark

Reputation: 801

Running separate ASP.NET projects on the same port

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

Answers (3)

kas_miyulu
kas_miyulu

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.

enter image description here

Upvotes: 1

UpQuark
UpQuark

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

genichm
genichm

Reputation: 535

I can suggest this way

  1. Create application for api in IIS
  2. Create application for MVC application in IIS
  3. 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>

    1. In both projects use custom web server for debugging VS 2012 it is: Right click on web project -> Web -> Select "Use custom web server" and provide the url of the project, it will be like localhost/api, localhost/mvc_client

From that moment your projects will have

  1. Same address in debug mode or when you are browsing to the project without debugging.
  2. Api project will allow cross domain requests.
  3. Client will able to send request to the API cause it allows cross domain requests.
  4. And you will able to debug the projects.

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

Related Questions