10

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

4 Answers 4

6

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

Sign up to request clarification or add additional context in comments.

1 Comment

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

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.

7 Comments

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. :/
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.
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?
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?
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
|
1

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.

4 Comments

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 Building a HTTP Web API as a service interface to an MVC site is almost always a waste of time, effort and performance.
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 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.
-1

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

1 Comment

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 :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.