Using the WebAPI Route Attribute

I have several methods that I want to use for their URLs.

Mostly there are restaurants that have ID cards, and a collection of terminals below them.

I'm trying to create the following kind of template: api / Restaurant - get all restaurants api / Restaurant / Bobs - get a restaurant with Bob ID api / Restaurant / Bobs / terminal - get all terminals in a restaurant bobs api / Restaurant / bobs / terminal / second - get terminal with the identifier of the second in the bob restaurant

I have methods for this, and I assigned each Route attribute the following:

    [HttpGet]
    public IEnumerable<IRestaurant> Get()
    {
        //do stuff, return all
    }

    [HttpGet]
        [Route("api/Restaurant/{restuarantName}")]
        public IRestaurant Get(string restaurantName)
        {
           //do stuff
        }

    [HttpGet]
    [Route("api/restuarant/{restaurantName}/terminals")]
    public IEnumerable<IMiseTerminalDevice> GetDevices(string restaurantName)
    {
       //do stuff
    } 

    [HttpGet]
    [Route("api/restaurant/{restaurantName}/terminals/{terminalName}")]
    public IMiseTerminalDevice GetDeviceByName(string restaurantName, string terminalName)
    {
        //do stuff
    }

However, only my main GET (api / Restaurant) works. My default webapi configuration and read

    config.MapHttpAttributeRoutes();

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

    config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));

- , ? ( ) 404.

!

+4
1

WEB API , ProductController. api.

  public class ProductsController:ApiController
    {



        [HttpGet]
        [Route("api/Restaurant/{restaurantName}")]
        public IHttpActionResult Get(string restaurantName)
        {
            //do stuff
            return Ok("api/Restaurant/{restuarantName}");
        }

        [HttpGet]
        [Route("api/restuarant/{restaurantName}/terminals")]
        public IHttpActionResult GetDevices(string restaurantName)
        {
            //do stuff
            return Ok("api/restuarant/{restaurantName}/terminals");
        }

        [HttpGet]
        [Route("api/restaurant/{restaurantName}/terminals/{terminalName}")]
        public IHttpActionResult GetDeviceByName(string restaurantName, string terminalName)
        {
            //do stuff
            return Ok("api/restaurant/{restaurantName}/terminals/{terminalName}");
        }
    }

, Fiddler,

**http://localhost:9969/api/restuarant/Vanbeo/terminals**

!

: Visual Studio 2013, WEB API 2.2, Net 4.5

?

PS: , !

+8

All Articles