J86
J86

Reputation: 15237

Calling Web API from MVC Application when both in same Solution

So I am totally new to the Web API and I thought to myself I would start a test project to learn it and AngularJS and how they can work together nicely ... etc.

I have created a multi-tier architecture within my solution, and inside my TPlan.API project, I have the following route configured in the Global.asax

GlobalConfiguration.Configuration.Routes.Add("default",
            new HttpRoute("api/{controller}"));

enter image description here

TPlan.WEB is my MVC application, and it is set up as "Start Up Project". When I run it, I want to be able to go to:

mysite:port/api/test

And get the value from the API from my test controller in there.

However it is not working for me, I get the following:

No HTTP resource was found that matches the request URI 'mysite:port/api/test'.

Upvotes: 10

Views: 11287

Answers (4)

spaceman
spaceman

Reputation: 1648

Er, in visual studio, right click on the solution and go to properties. Go to startup and select the "multiple projects" option and tick the website and the webservice to both start up.

That will start both of them. But as has been pointed out, they're running on separate ports, they're completely separate applications... I don't think you can register a route that belongs outside of the website.

We work like this

View => POST / GET => MVC Controller => HTTP Request => API Controller

So our mvc views post or get to our mvc controllers, and then we fire off a separate http request to the web api. this is a server to server call, its like calling any external web service. we wait for the response from the api and then return whatever is required to the view...

Upvotes: 6

Darrel Miller
Darrel Miller

Reputation: 142014

Make your TPlan.API project a simple Assembly, reference it from TPlan.Web and pass the GlobalConfiguration.Configuration property over to a Register method that is in your API assembly. When the MVC project starts, both the MVC routes and the Web Api routes will be active on the same web host.

Here is an example of an API that has both a console host and a web host in the same solution.

Upvotes: 1

Claies
Claies

Reputation: 22323

What you are attempting isn't logically possible without installing your WebAPI project into IIS ahead of time, which I'm sure is not what you want. Both projects cannot be run at the same time, as only one project will be able to launch a debug session of IIS Express. Even if both projects were able to run at the same time, they would be on different logical ports, and routing from one project would have to be manually sent to the listening port of the other instance. Basically, your Global.asax on your API project will never run, as that project will be built as a class library.

Upvotes: 2

Gjohn
Gjohn

Reputation: 1281

Please check the following site. I believe the issue lies in the configuration of the route

http://www.asp.net/web-api/overview/extensibility/configuring-aspnet-web-api

Upvotes: -1

Related Questions