Reputation: 8531
I have created a solution with the following project
FrontEnd is a mvc4 web application and MyWebAPI is a mvc4 webAPI
I want to be able to access MyWebAPI control methods from my FrontEnd project. But it doesn't work as it only complains that data couldn't be found when I run the webAPI urls.
Do I have to do something more so my FrontEnd can talk to MyWebAPI?
For example I simply just tried to run this pre generated webAPI control method
public string Get(int id)
{
return "value";
}
It could not be found. However if I change the startup project to be MyWebAPI then it works. So my question is how to make it so I can access MyWebAPI from my FrontEnd project?
This question is actually same as this one but it doesn't explain how I can access the webAPI from the frontend when they are within the same solution
EDIT
$(document).ready(function() {
$("#clickMe").click(function() {
var id = $("#dataBox").val();
$.ajax({
url: "http://localhost:11982/api/Movie/GetMovie",
data: { id: id },
type: "get",
contentType: "application/json;charset=utf-8",
success: function(data) {
alert(data);
},
error: function(jqXHR, status, errorThrown) {
// alert("Error " + status + "\nError Thrown" + errorThrown + "\n" + jqXHR )
}
})
})
})
UPDATED 2012-08-30
I updated different packages in my solution, amongst them WebAPI beta to WebAPI rc version. And now for some reason when I send a form request or ajax request to any of the WebAPI controller, it will for the form request response saying that no such controller was found, and for for the ajax request it will load forever without returning a response.
Anyone who experience similar problem before?
Previous solution to it was to run my FrontEnd and MyWebAPI in local IIS server. However it stopped to work now since I updated all the packages.
Upvotes: 2
Views: 942
Reputation: 12680
Seems like your issue is you need to run both the FrontEnd & MyWebAPI projects simultaneously from Visual Studio. It is actually easy to configure Visual Studio to do this with a somewhat obscure start up option of the solution.
Right click in the solution node (not your project node with the same name) in the Solution Explorer pane. From the context menu item, select "Set Startup Projects". In the dialog, click the "Multiple Startup Project" option and select both the FrontEnd & MyWebAPI projects. Click OK to save the options. Both project should now start up when you run the debugger and the MyWebAPI calls will respond to the FrontEnd. Just make sure both projects are running on different ports.
Update:
There were breaking changes in the Web API RTM. One of the changes was the conventions used for extracting ApiController
action parameters. Look at how the new attributes FromUri
and FromBody
are used in this SO question and answer
Upvotes: 2