Nair
Nair

Reputation: 7458

Why Web API is not finding my resource?

I am using Web API 2 and developing in ASP.Net 4. This is a sample code that I trying to learn webapi. There are two routes. The first route is services resource for a given store. The second route is store resource route.

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

    routes.MapRoute(
        name: "Services",
        url: "store/{id}/services",
        defaults: new { controller = "Services" }
    );

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

The second route "Store" works perfectly. The first route is to go in detail of all the services available in the store. When I try

/api/store/1/services

I get 404 error. Could someone point me to what am I doing wrong?

Here is the controller

namespace APITestter1.Controllers
{
    public class ServicesController : ApiController
    {
        public string Get(int id, string prop = "xxx")
        {
            return "Hello services World!" + id + " added attribute " + prop;
        }

        public string Post(int id, string prop = "xxx")
        {
            return "Hello Post World!" + id + " added attribute " + prop;
        }

    }
}

Upvotes: 0

Views: 547

Answers (2)

Nkosi
Nkosi

Reputation: 246998

You are mapping your web api routes in the wrong file. If you are doing web api then look for WebApiConfig.cs file and map your web api routes there.

You are also trying to browse to /api/store/1/services but in your example you map the route as store/{id}/services. Note you don't have api in your route template.

The following web api config should match what you are trying to do

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Attribute routing.
        config.MapHttpAttributeRoutes();

        // Convention-based routing.
        config.Routes.MapHttpRoute(
            name: "ServicesApi",
            routeTemplate: "api/store/{id}/services",
            defaults: new { controller = "Services" }
        );

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

You also say your second route "Store" works perfectly but have not showed what URL you used. Assuming it was /api/store/1 then it would work because it maps to the DefaultApi route template which uses api/{controller}/{id}.

Upvotes: 1

Fan TianYi
Fan TianYi

Reputation: 417

I do not understand why the prefix plus "api" and "prop"?

I update your code:

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

        routes.MapRoute(
            name: "Services",
            url: "api/store/{id}/services/{prop}",
            defaults: new { controller = "Services", action = "store", prop = UrlParameter.Optional }
        );

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


namespace APITestter1.Controllers
{
    public class ServicesController : ApiController
    {
        [HttpGet, ActionName="store"]
        public string Get(int id, string prop = "xxx")
        {
            return "Hello services World!" + id + " added attribute " + prop;
        }

        [HttpPost, ActionName="store"]
        public string Post(int id, string prop = "xxx")
        {
            return "Hello Post World!" + id + " added attribute " + prop;
        }

    }
}

Upvotes: 1

Related Questions