Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用WebAPI路由属性_C#_Asp.net Mvc_Asp.net Web Api_Asp.net Mvc Routing - Fatal编程技术网

C# 使用WebAPI路由属性

C# 使用WebAPI路由属性,c#,asp.net-mvc,asp.net-web-api,asp.net-mvc-routing,C#,Asp.net Mvc,Asp.net Web Api,Asp.net Mvc Routing,我有一些方法,我希望它们的URL遵循特定的模式 基本上有餐馆,有ID,下面有一系列终端 我试图让下面的模式出现: api/餐厅-获取所有餐厅 api/餐厅/Bobs-获取ID为Bobs的餐厅 api/餐厅/Bobs/终端-获取Bobs餐厅中的所有终端 api/Restaurant/bobs/terminals/second-获取餐厅中ID为second的终端 我已经有了实现这一点的方法,并且我已经为每个方法指定了Route属性,如下所示: [HttpGet] public IEn

我有一些方法,我希望它们的URL遵循特定的模式

基本上有餐馆,有ID,下面有一系列终端

我试图让下面的模式出现: api/餐厅-获取所有餐厅 api/餐厅/Bobs-获取ID为Bobs的餐厅 api/餐厅/Bobs/终端-获取Bobs餐厅中的所有终端 api/Restaurant/bobs/terminals/second-获取餐厅中ID为second的终端

我已经有了实现这一点的方法,并且我已经为每个方法指定了Route属性,如下所示:

    [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
    }
有人知道我哪里出错了吗?所有其他方法返回路由不匹配(ID为的餐厅)或404


提前谢谢

我刚刚创建了默认的WEB API项目,其中包括一个ProductsController。接下来,我粘贴了您的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

请使用空项目重试,好吗


附言:我必须把这个作为一个答案,因为评论中没有足够的空间

你所说的
和读取config.maphttpAttribute()是什么意思?…您正在调用
config.maphttpAttribute路由()
,因为这是一个探测控制器并在路由表中注册属性路由的路由…还要注意,与常规路由匹配的请求永远不会与用属性路由修饰的控制器/操作匹配。@Mathieson您可以添加post请求吗?我已经用默认模板试用了您的API,效果很好。是的,该部分是WebApiConfig类的值-修复了格式设置以使其更清晰。@Toan-sure!url返回正确的JSON。url触发控制器构造函数,并返回“{”消息“:“未找到与请求URI匹配的HTTP资源”“在与请求匹配的控制器“餐厅”上找到了操作”“}”url仅返回404,而没有触发控制器构造函数。“restuarant”键入可能没有帮助。我创建了一个新的WebAPI项目,并用上面的代码替换了ValuesController方法-现在我发现返回正确。但是,返回4040。您可以将Web API包升级到2.2版,然后重试吗?WebAPI的版本为2.2。我认为方法名称有问题-将签名更改为[Route(“api/restaurantName}/{terminalName}”)][Route(“api/restaurantName}/terminals/{terminalName}”)][HttpGet]public IHTTPacationResult GetTerminalByName(string restaurantName,string terminalName)至少使我能够使用该方法。我已经进行了重构,以转移到一个更大的餐厅聚合根目录,因此这是一个回避问题的好方法。但仍然令人沮丧——即使是RoutedBugger也没有表现出太多!
**http://localhost:9969/api/restuarant/Vanbeo/terminals**