使用WebAPI路由属性 [英] Using WebAPI Route attribute

查看:112
本文介绍了使用WebAPI路由属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几种方法,我想遵循其网址的特定模式.

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

基本上有一些餐厅,这些餐厅都有ID,并在其下面有一系列的终端机.

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

我正在尝试出现以下几种模式: api/餐厅-获取所有餐厅 api/Restaurant/Bobs-获取具有Bobs ID的餐厅 API/餐厅/鲍勃/码头-获取鲍勃餐厅的所有码头 api/Restaurant/bobs/terminals/second-在餐厅bob中获取ID为second的终端

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

我已经拥有执行此操作的方法,并且已将Route属性分配给每个属性,如下所示:

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
    }

但是,只有我的基本GET(API/餐厅)可以正常工作.我的WebAPI配置是默认配置,读取为

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

有人知道我要去哪里了吗?所有其他方法都返回路由不匹配(​​带有ID的餐厅)或404.

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

提前谢谢!

推荐答案

我刚刚创建了默认的WEB API项目,其中包括一个ProductsController.接下来,我将您的api方法粘贴到其中.

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

最后,我用Fiddler进行了请求

Finally, I used Fiddler to make an request

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

一切正常!

系统配置:Visual Studio 2013,WEB API 2.2,Net 4.5

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

您可以重试一个空项目吗?

Could you please retry with an empty project?

PS:我必须将其发布为答案,因为评论中没有足够的空间!

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

这篇关于使用WebAPI路由属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆