Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 290
UpQuark Avatar asked Nov 16 '25 04:11

UpQuark


1 Answers

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.

like image 145
genichm Avatar answered Nov 17 '25 17:11

genichm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!