Francis Groleau
Francis Groleau

Reputation: 344

Calling an MVC 3 Apicontroller from another website

I have a mvc 3 API controller in my project

namespace Carabistouille.Controllers
{
    public class CarabistouilleController : ApiController
    {
        /// <summary>
        /// Method to retrieve all of the carabistouille in the catalog.
        /// Example: GET api/carabistouille
        /// </summary>
        [HttpGet]
        public HttpResponseMessage Get()
        {
            IEnumerable<Carabistouille.DB.Carabistouille> list = Carabistouille.DB.CarabistouilleDAL.GetAllCarabistouille();
            if (list != null)
            {
                return   Request.CreateResponse<IEnumerable<Carabistouille.DB.Carabistouille>>(HttpStatusCode.OK, list);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }
        }
    }
}

and I want other website to be able to query that API to get the data so I do something like this :

        $("#query_results").empty();
        $("#query_results").append('<table id="ResultsTable" class="BooksTable"><tr><th>Description</th><th>Auteur</th><th>Date</th></tr>');
        $.ajax({
            type: "get",
            contentType: "application/json; charset=utf-8",     

            url: "http://www.Carabistouile.ca/api/carabistouille",
            success: function(msg) {
                    var c = eval(msg.d);
                    for (var i in c) {
                            $("#ResultsTable tr:last").after("<tr><td>" + c.Description + "</td><td>" + c.Auteur + "</td><td>" + c.Date + "</td></tr>");
                        }
                }
        });

Well all I get is :

Failed to load resource: net::ERR_NAME_NOT_RESOLVED

Here is my project architecture

enter image description here

What am I missing ?

Upvotes: 0

Views: 116

Answers (1)

Louis
Louis

Reputation: 583

The url you've given in the example returns a 404, which probably means that the routes this controller is supposed to handle have not been registered with the application

Have a look at How to add Web API to an existing ASP.NET MVC 4 Web Application project?

In essence you need to call

WebApiConfig.Register(GlobalConfiguration.Configuration);

In the Application_Start method in the global.asax.cs and make sure your configuration includes the routes this controller is meant to handle

Upvotes: 1

Related Questions