Join the Stack Overflow Community
Stack Overflow is a community of 6.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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

share|improve this question
up vote 1 down vote accepted

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.

share|improve this answer
    
Hmm so how do people that build AJAX heavy applications go about testing them? I was hoping to make AngularJS call the data and get JSON back from my Web API, but all within the same solution. :/ – Ciwan Jan 25 '14 at 21:58
    
in every project I've seen, the WebAPI is in the Web Project, not a separate class library. if your project is mostly Angular.js anyway, there shouldn't be enough other MVC pages that the WebAPI and MVC would conflict. – Claies Jan 25 '14 at 22:02
    
I see. So if I move my Web API into the Web MVC Project, that can work correct? And my API can serve others' data for consumption? – Ciwan Jan 25 '14 at 22:03
    
Also, how does one go about splitting the API logic from the main MVC app? Would I create a separate area to house all API related classes? – Ciwan Jan 25 '14 at 22:09
    
you can certainly have a separate class library that all your API controllers call functions from, but the actual controllers and the routing logic would still live in the MVC project – Claies Jan 25 '14 at 22:48

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

share|improve this answer
    
Thanks for this nugget. – WickedW Jul 15 '14 at 16:04
1  
Well put. Just a side point, you can just right-click the Web API Project and choose Debug > Start, then do the same for the MVC project. – Suamere Oct 31 '14 at 19:56

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.

share|improve this answer
    
This brings your Stateless RESTful API backward into the mindset of older web services. One of the allures of Web API is that your UI's don't have to reference the Assemblies. And though old SOAP or ASMX web services were correct to have service-references, bringing that mindset to Web API almost feels like breaking Separation of Concerns. Granted, this could just be a workaround to debugging, and in production the two are separated thus making for a good practice. But this almost seems to enable a possible bad habit. – Suamere Oct 31 '14 at 20:03
    
@Suamere Building a HTTP Web API as a service interface to an MVC site is almost always a waste of time, effort and performance. – Darrel Miller Nov 1 '14 at 1:46
    
That is a powerful statement. I haven't seen waste in my own experience. I've never heard that before, and haven't yet found information supporting that statement since you said it. Could give a link to some discussion that elaborates on that? The way I understand it is: You should either have a different Web API project on an api subdomain, or if it remains in your MVC site, should be completely alienated in its own subfolder of the project, not mixed with the MVC. Both ways should have the same performance, and it seems easier to maintain the seperate layer. – Suamere Nov 3 '14 at 15:28
    
@Suamere Wait until a third party takes a dependency on your API and then see how much pain you are going to experience when you try and update your MVC site with breaking that third party. – Darrel Miller Nov 3 '14 at 16:42

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

share|improve this answer
    
Thanks, notice screenshot of solution that I added. I have read of the link, but it still did not resolve the issue I'm having :( – Ciwan Jan 25 '14 at 21:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.