john doe
john doe

Reputation: 9700

ASP.NET Web API Route not found

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);

        }

Upvotes: 6

Views: 20203

Answers (4)

JoeyZhao
JoeyZhao

Reputation: 543

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.

Upvotes: 16

frich
frich

Reputation: 145

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 }
    );
});

Upvotes: 1

cheesesharp
cheesesharp

Reputation: 543

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
    }

Upvotes: 3

Ken Brittain
Ken Brittain

Reputation: 2265

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.

Upvotes: -1

Related Questions