Insert web api class into existing web project

Asked

Viewed 431 times

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

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

When I do exactly the same thing on a new web project, the system runs smoothly.

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

Follows details:

Method of Global.asax.Cs

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: inserir a descrição da imagem aqui

ValuesController.cs

inserir a descrição da imagem aqui

Project structure

inserir a descrição da imagem aqui

  • 1

    Post more details of the error. You can create a web project with API specification becomes easier. Is your ajax request post or get? your method accepts both? Generally the error "failed to find the resource you needed" happens for several reasons, post the method code. Try to clean the project and restart the visual studio. Sometimes it gives error in recompiling and ends up saying that the feature does not exist!

  • Oops. So, starting a new project won’t help me, because I really want to make some improvements in this one that already exists. When I create a new one, it runs smoothly. I will post the code.

  • Post the method you are testing and your ajax request!

  • I posted, but as I said, if I create a new solution, with a web project and put exactly the same code with the same structure, it works perfectly.

  • When you return json content?

  • Premiere, thanks for the help. My mistake was on my route, which did not have the address of the subfolder. My routeTemplate should look like this: "View/api/{controller}/{id}". Anyway, thank you very much for your attention.

  • json is the date Premiere.

Show 2 more comments

1 answer

3


My mistake was in not placing the subfolder (View) of my aspx.Cs file on the routeTemplate.

My method should stay 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 }
    );
}
  • When you make a request with jquery, JSON, it checks the content-type and if it is not the same as application/json it will not return. The error of the resource is not for that reason, just to warn! If you really need to use the API within the web app project from a glance at the SELF HOST WEB API, you might be more suited to your needs.

  • Vlw Premiere. Thanks for the tip.

  • Then mark your answer as the correct one!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.