.netiismodel-view-controllerhosting

How to Host Web API and MVC application on Same Address


I have one .NET Solution which has two projects one for MVC app and the other one for APIs.

I need one help regarding the hosting on IIS, where I can deploy both app on same server without changing the address or port.

I wanted to host both app in such way so that I don't need to change the API ajax URL in MVC application. What I means is:

In MVC, to call the Web API I have used ajax call (url:"/api/LoginAPI/CheckUser") Note: I have 50+ ajax call on application.

I want to make ajax call by simply adding "/API/" as a suffix and it should be able to call my API and respond as expected result without any issues like (API not found or path is not correct)

I am bit sure that it could be possible but need more expert advice to make sure that I am thinking in a right way.

Please provide your valuable feedback.


Solution

  • As you say , you have two projects ,one is API and second is MVC . API project do also support all .net MVC Features like it does gives all default references of MVC (library refernce) . API project includes all MVC dll by default

    1st Way

    You just copy following elements in your API project .

    All Models, Controllers All Views All CSS Files All JS Files all the refence of mvc project and give this to API project .

    Also dont forget to include in your project . This will works for make this single project .

    2nd Way

    Just Build your MVC project and give refence to API Project . and Move Views CSS JS in your API project .

    3rd Way .

    you can create Virtual Directories to have two application on single domain.