Technologies used:
I've been wrestling with the idea of having a Web API project and a separate web site project in a single solution.
Now by default web api doesn't allow cross origin policies. So I played around with enabling CORS in my Web API 2, although I was able to get it to work, it only works for the latest browsers; I need the backward compatibility of IE7 to IE9.
So I played around with JSONP. I'm not fond of the lack of support that exists for this. I was able to get it to work for my Web API 2 project, but it doesn't work if I wanted to use BreezeController if using the breezejs web api library. It also doesn't work if I wanted to create an ODataController.
So I'm moving away from the idea of cross origin sharing; though hoping that in the future there will be enough support for jsonp regardless if I use BreezeJS WebAPI helper or ODataControllers.
For now, I have no idea how to put my WebAPI project and my MVC 5 Website under the same domain where I can have:
Do I have to make some configuration in my host file? if I want to run my projects from VS2013 would it be able to run both projects under the same domain.. or do I have to keep on manually changing the URL in the browser?
Well, the answer was really really simple.
I know this an old question, it was just that I forgot how to do it since it's been so long since I did this. Searching for the answers on google and on stackoverflow was difficult since the discussions talked about setting up cross origin policies instead of setting up a same origin policy.
I spent the good portion of my time putting everything on IIS.
Created a website and had it point to the physical path where my website csproj and bin folder is located. I gave it a hostname of "dev.example.com" and changed my hostfile for 127.0.0.1 to refer to dev.example.com
Created a Web Application for the website, and set it up for web api 2 project. Everything magically worked after that.
The dumb part was, I could have easily done this in visual studio. I remember in VS2010, this cause many problems, but in 2013, I guess bugs have been fixed and it works now.
I had my WebSite project set up as
localhost:2020/ExampleProject.WebSite (incorrect)
instead of
localhost:2020 (correct)
and created a virtual directory. I do this by right clicking the project, going to properties, under "Web" tab.
So basically, the rule of thumb was to get my website to be my main root domain, and to copy that full domain to any web application I want to add under it.
So: website would be:
localhost:2020
web application:
localhost:2020/ExampleProject.API
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With