【问题标题】:MVC4: How to control multiple parameters [duplicate]MVC4:如何控制多个参数[重复]
【发布时间】:2012-07-30 13:01:43
【问题描述】:

可能重复:
Routing with Multiple Parameters using ASP.NET MVC

使用 MVC4 web api 进行实验,我在 Global.asax 中定义了以下路由

routes.MapRoute(
  name:="API Default", 
  url:="api/{systemid}/{controller}/{id}",
  defaults:=New With {.id = RouteParameter.Optional}
)

我相应地更改了控制器

Public Class ValuesController
    Inherits ApiController

 Public Function GetValues(systemid As Integer) As IEnumerable(Of String)
     ---
 End Function

 Public Function GetValue(systemid As Integer, ByVal id As Integer) As String
     ---
 End Function
End Class

我正在寻找格式请求 uri 像

http://localhost/api/13/values/5 

但唯一有效的调用是使用显式参数,至于

http://localhost/api/values?id=5&systemid=4

有没有办法完成我正在寻找的东西?

【问题讨论】:

  • 这似乎部分重复,使用错误的类出现错误,正如@cuong所指出的那样

标签: asp.net rest asp.net-mvc-routing asp.net-mvc-4 asp.net-web-api


【解决方案1】:

我猜你用错了路由类,不是用Web API路由,而是用了MVC路由,应该是:

routes.MapHttpRoute(
  name:="API Default", 
  routeTemplate:="api/{systemid}/{controller}/{id}",
  defaults:=New With {.id = RouteParameter.Optional}
)

【讨论】:

  • 谢谢,就是这么简单......
猜你喜欢
  • 1970-01-01
  • 2013-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多