6

I'm not sure why but my api route just isn't working? What I do is click on the add controller, select Web API 2 controller with actions using EF (so that it builds it for me) select the model and context and click Add.

WebApiConfig

public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }

ProductsController (ApiController)

public IQueryable<Product> GetProducts()
    {
        return db.Products;
    }

This would imply that by going to /api/products it would display a json format of products ... it doesn't. What I DO get is a 404: The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. I tried putting a breakpoint in too and that doesn't even get hit. So I'm a little confused.

I do have custom Web Route configs, which I thought could have been the problem, but when commenting them out still this happens. Is scaffolding missing something?

Thanks for any help

5
  • In the official examples they use IEnumerable instead of IQueryable. maybe this is the issue? Commented Feb 19, 2015 at 20:02
  • 1
    Did you add/create the WebApiConfig after the project was created? If so, did you remember to add it to the Global.asax where the other registration events occur? It's worth a check if you manually tried to setup the Web API after the project was created. Commented Feb 19, 2015 at 20:21
  • I believe it was automatically scaffolded for me. What do I need to look for in the Global.asax file? I have RouteConfig.RegisterRoutes(RouteTable.Routes) but nothing specifically for API. Could that be the problem? Commented Feb 19, 2015 at 20:39
  • SORTED! Thank you Nick for pointing me in the right direction. It was indeed something to do with Global.asax file. I have updated the question with the fix Commented Feb 19, 2015 at 21:20
  • 1
    Don't post the solution in the question and put "RESOLVED" in the title. Post it as an answer and accept it. Then fix your title so it actually describes the problem. Commented Feb 19, 2015 at 21:27

2 Answers 2

22

After creating a new project and creating an API controller a text file popped up (One i decided to close thinkign I knew better) But it does not pop up again once closed.

The Solution

Simply add 2 using statements to Global.asax

using System.Web.Routing;
using System.Web.Http;

and GlobalConfiguration.Configure(WebApiConfig.Register); MUST COME FIRST in the same file under the Application_Start method

protected void Application_Start()
    {
        GlobalConfiguration.Configure(WebApiConfig.Register); // <--- this MUST be first 
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }

Hopefully this can help someone else out

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

1 Comment

Same... I copied the line and closed the file. It is clearly written to put it at the beginning of Application_Start(). Thanks for answering your own post.
2

A little addition to Ultigma's answer. In your global.asax.cs this:

GlobalConfiguration.Configure(WebApiConfig.Register);

should be prior to this:

RouteConfig.RegisterRoutes(RouteTable.Routes);

The reason is that the default Web Api route "api/{controller}/{action}/{id}" is more specific than the MVC default route "{controller}/{action}/{id}" and should be placed first. If not, a request to /api/products will be treated as a call to a Products method of an Api controller.

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.