2

I have created a solution with the following project

FrontEnd is a mvc4 web application and MyWebAPI is a mvc4 webAPI

enter image description here

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.

Community
  • 1
  • 1
starcorn
  • 8,261
  • 23
  • 83
  • 124

1 Answers1

2

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

Community
  • 1
  • 1
Sixto Saez
  • 12,610
  • 5
  • 43
  • 51
  • I tried it now, and it still doesn't work. They are on different ports, but the same problem still exist. I can access webAPI from the views in that projects. But by using the views from my Frontend I cannot. I added the script I'm using to fetch data – starcorn Jun 04 '12 at 16:34
  • That behavior sounds more like an issue with the routing configuration. To troubleshoot this, you could make both project run as separate web sites if you have either IIS or IIS Express install on your machine. From the project properties tabs (double-click the Properties node in the project), select the Web tab. Choose the "Use Local IIS Web Server" and enter a name for the web site root. Visual Studio will create the web site in IIS for you. Test each web site separately after you create it. If that works, you should be able to call MyWebAPI URLs from FrontEnd using the web site URL. – Sixto Saez Jun 04 '12 at 17:21
  • thanks it works now! I changed the web properties for both of the project so it uses IIS instead for the development server. – starcorn Jun 04 '12 at 18:04
  • Sorry, I repopend this question. Because since I updated the packages in my solution it stopped working. I can access the webapi if I simply input the URL to the address bar in a web browser. However it does not work if I send a request by a form or if I send an ajax request. – starcorn Aug 30 '12 at 11:13