6

I have just added a new Web API controller to my project. Now, I am trying to invoke the controller from JavaScript. But first I am also interested to manually invoke the route which is not pulling up anything. It says "Page Not Found".

My RegisterRoutes methods is shown below:

 public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "DefaultApi",
                url: "api/{controller}/{id}",
                defaults: new { id = UrlParameter.Optional }
            );

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

My API controller is called FlightsController:

 public class FlightsController : ApiController
    {
        public IEnumerable<FlightViewModel> GetAllFlights()
        {
            return new List<FlightViewModel>() { new FlightViewModel() { Name = "Some Flight" }};
        }
}

I am trying to access it using the following URL:

http://localhost:54907/api/flights // The resource not found
http://localhost:54907/MyProject/api/flights // resource not found 
api/flights // name not resolved 

What am I doing wrong?

VERSION: I added a new file to my controllers directory which is called "Web API Controller Class (v2.1)

UPDATE: Here is my updated web.config

protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);

            GlobalConfiguration.Configure(WebApiConfig.RegisterRoutes);

            BundleConfig.RegisterBundles(BundleTable.Bundles);

        }
7
  • Tell us which version of the WebApi/MVC framework you are using so we can help you better. WebApi has gone through many changes that can have an impact on this problem. Commented Apr 16, 2015 at 11:10
  • Web API Controller (v2.1) , .NET 4.5, ASP.NET MVC runtime version 4.0 Commented Apr 16, 2015 at 11:12
  • Where do you call RegisterRoutes(..) from? Commented Apr 16, 2015 at 11:18
  • RegisterRoutes is called from inside the Global.asax file. This is the default implementation and I have not changed it. Commented Apr 16, 2015 at 11:19
  • Are you trying to access the controller or action? Commented Apr 16, 2015 at 11:31

4 Answers 4

16

I was having the similar issues and I decided to download the example from this website and compare the difference. Eventually, I fixed my issue by switching the sequence of the route registering function calls in Global.asax.cs file.

Instead of :

RouteConfig.RegisterRoutes(RouteTable.Routes);
GlobalConfiguration.Configure(WebApiConfig.RegisterRoutes);

Call the WebApiConfig.RegisterRoutes first, that is:

GlobalConfiguration.Configure(WebApiConfig.RegisterRoutes);
RouteConfig.RegisterRoutes(RouteTable.Routes);

I'm still investigating the reasons behind. But hope this could help.

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

1 Comment

I had a similar issue and it was fixed by changing the order. Thx !
3

I suggest using HttpAttributeRoutes

Decorate your controller

    [RoutePrefix("api/flights")]
    public class FlightsController : ApiController
    {
       [Route("")]
        public IEnumerable<FlightViewModel> GetAllFlights()
        {
            return new List<FlightViewModel>() { new FlightViewModel() { Name = "Some Flight" }};
        }
    }

Register your routes...

public static class WebApiConfig
    {
        #region Public Methods and Operators       

        public static void RegisterRoutes(HttpConfiguration config)
        {
            // Web API routes
            config.MapHttpAttributeRoutes();
        }

        #endregion
    }

Comments

1

You are looking for a MVC Controller when you put the route in RouteConfig.cs. You should move the api route to App_Data/WebApiConfig.cs.

RouteConfig:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

WebApiConfig:

config.MapHttpAttributeRoutes();

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

Edit: It doesn't really matter where the action is located. As long as you call it from Application_Start() in Global.asax.cs you could just add something like

GlobalConfiguration.Configure((config) => 
{
    config.MapHttpAttributeRoutes();

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

16 Comments

I cannot add any files to App_Data folder manually. And currently that folder is empty.
I am invoking using localhost:54907/MyProject/api/flights it still does not return anyting
take out MyProject while using IISExpress.
I did and still The resource cannot be found.
Wait! The RegisterRoutes on WebApiConfig is not getting called. Where should I call it
|
-1

Your API route does not have an action defined and the routing engine cannot get you there:

http://localhost:54907/api/flights // The resource not found

This route does not have an action defined or a default. The routing engine will take you to a controller but has not action (method) to map to.

http://localhost:54907/MyProject/api/flights // resource not found 

Unless you mapped the application to "MyProjects" in IIS or similar you will run into the same problem as above.

3 Comments

How can I define what I am missing?
Change the routing url: "api/{controller}/{action}/{id} and call http://localhost:54907/api/flights to see it function. Next you can change GetAllFlights to 'Flights' to keep with your desired api.
Same issue as I wrote in the original post. The page does not exist.

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.