Insert web class api into existing web project

2

In my visual studio 2012, I have a solution with several projects and one of them is web. In this project, I would like to include a "Web Api Controller" class to do some testing.

I created a class with the name ValuesController, with all the necessary methods. I created a simple .aspx page and put the ajax code to call the api, but the browser gives the answer that it could not find the resource it needed (/ View / api / Values).

When I do exactly the same thing in a new web project, the system runs without problem.

Has anyone ever faced this problem or even managed to help?

Follow the details:

Global.asax.cs Method

void Application_Start(object sender, EventArgs e)
    {
        RouteTable.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = System.Web.Http.RouteParameter.Optional }
        );
    }

Jquery function for calling the server method:

ValuesController.cs

Project Structure

    
asked by anonymous 20.08.2014 / 04:06

1 answer

3

My error was in not putting the subfolder (View) from my aspx.cs file in routeTemplate.

My method should look like this:

void Application_Start(object sender, EventArgs e)
{
    RouteTable.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "View/api/{controller}/{id}",
    defaults: new { id = System.Web.Http.RouteParameter.Optional }
    );
}
    
20.08.2014 / 04:47