Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web API 2 project and MVC 5 Website project in same domain

Technologies used:

  • BreezeJS
  • OData
  • Web API 2
  • MVC 5
  • IDE: Visual Studio 2013

I've been wrestling with the idea of having a Web API project and a separate web site project in a single solution.

  • My Web API 2 project opens up as: localhost:2020/ExampleProject.API
  • My MVC 5 WebSite project opens up as: localhost:5050/ExampleProject.WebSite

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:

  • localhost/ExampleProject.API
  • localhost/ExampleProject.WebSite

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?

like image 336
sksallaj Avatar asked Jan 26 '14 23:01

sksallaj


1 Answers

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.

  1. 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

  2. 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

like image 82
sksallaj Avatar answered Oct 03 '22 09:10

sksallaj