Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using WebAPI Route attribute

I've got a few methods, which I want to follow a specific pattern for their URLs.

Basically there's restaurants, which have IDs, and a collection of Terminals under them.

I'm trying to get the following sort of pattern to emerge: api/Restaurant - get all Restaurants api/Restaurant/Bobs - gets the restaurant with the ID of Bobs api/Restaurant/Bobs/terminals - get all terminals in bobs restaurant api/Restaurant/bobs/terminals/second - get the terminal with the ID of second in the restaurant bob

I've got the methods to do this, and I've assigned the Route attribute to each as follows:

    [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 basic GET (api/Restaurant) is working. My WebAPI config is the default, and reads

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

Anybody know where I'm going wrong? All other methods return routing mismatch (restaurant with ID) or a 404.

Thanks in advance!

like image 445
Mathieson Avatar asked Aug 04 '14 02:08

Mathieson


People also ask

How do I specify a route in web API?

The Route attribute can be applied on any controller or action method. In order to use attribute routing with Web API, it must be enabled in WebApiConfig by calling config. MapHttpAttributeRoutes() method.

How can use route attribute in ASP NET MVC?

Enabling Attribute Routing in ASP.NET MVC Enabling attribute routing in your ASP.NET MVC5 application is simple, just add a call to routes. MapMvcAttributeRoutes() method with in RegisterRoutes() method of RouteConfig. cs file. You can also combine attribute routing with convention-based routing.

What is route in REST API?

RESTful routing is a set of standards used in many different languages to create efficient, reusable routes. It aims to map HTTP methods (GET, POST, PATCH, DELETE) and CRUD actions (Create, Read, Update, Destroy) together in a conventional pattern.


1 Answers

I have just created the default WEB API project which includes a ProductsController. Next, I pasted your api methods in.

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

Finally, I used Fiddler to make an request

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

and everything works fine!

System Configs: Visual Studio 2013, WEB API 2.2, Net 4.5

Could you please retry with an empty project?

PS: I have to post this as an answer because there is not enough space in the comment!

like image 136
Toan Nguyen Avatar answered Sep 19 '22 01:09

Toan Nguyen