0

I'm new to Web API and followed some examples, but I cannot work out where the Controller Name is specified.

I have this in my global.asax:

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

I have a Controller called TestAPI.cs

and this is the contents:

namespace Testing
{
    public class TestAPI : ApiController
    {
        // GET api/<controller>
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/<controller>/5
        public string Get(int id)
        {
            string test= "Hello!";
            return test
        }
    }
}

I've only put "return test" in there as an example for Stack Overflow as it contains a lot of code.

So if I am consuming this in jQuery, what path would I use for the controller?

3
  • your controller name should be TestAPIController.cs Commented Feb 12, 2014 at 19:50
  • Controller can be located anywhere in the project but it is recommended to keep it under Controllers. The file name can be anything. Do search on entire project for class TestAPIController. Commented Feb 12, 2014 at 19:51
  • hmm, I seem to keep getting a 404 error message for anything I try? Commented Feb 12, 2014 at 19:55

4 Answers 4

5

Aside from the naming convention about your Web Api controller name, you also need to change the route configuration. see below:

1) Change your TestAPI controller like below:

 public class TestAPIController : ApiController

2) change MapHttpRoute in your Application_Start method like below:

    GlobalConfiguration.Configuration.Routes.MapHttpRoute(
      name: "API Controller",
      routeTemplate: "api/{controller}/{id}",
      defaults: new { id = RouteParameter.Optional }
      );

Note: Don't forget also change the actual file name form TestAPI.cs to TestAPIController.cs.

Sign up to request clarification or add additional context in comments.

5 Comments

Hi Lin, I have changed this as per your answer but still no luck! Gives me a 404! Thanks
hi @realtek, I think you forgot change the file name TestAPI.cs to TestAPIController.cs, see my updated answer.
Hi Lin, Yes I also did this but still no luck. Could you confirm what the file system structure should be? So does TestAPIController.cs see to be inside api/Controllers/TestAPIController.cs for example? Or does it not actually look at any filesystem? Thanks
The TestAPIController.cs file should be inside the Controller folder. If not you need to change your route.
Thank you lin! I got this working now, I just read up on asp.net/web-api/overview/web-api-routing-and-actions/… I didn't realise you need to drop the "Controller" bit from the client request. Its automatically added onto the end of the {controller} placeholder. It works now thanks
1

Rename your controller to TestApiController and remove the {action} parameter from the route.

Comments

0

That should just be

http://[yourserver]/[yourapp]/api/testapi

or

http://[yourserver]/api/testapi

Depending on how you set up your application

1 Comment

hmm, I seem to keep getting a 404 error message for anything I try?
0

Isn't your controller name supposed to end in "Controller" even if it's a WebApi controller?

I think

public class TestAPI : ApiController

should be

public class TestAPIController : ApiController

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.