4

我正在关注本教程:

http://www.asp.net/web-api/overview/web-api-routing-and-actions/create-a-rest-api-with-attribute-routing

并查看“获取图书详细信息”部分。

我看到这段代码:

[HttpGet("api/books/{id}/details")]
public BookDetailDto GetBookDetail(int id)
{
}

但是我收到的错误是

HttpGet 有 0 个参数,但使用 1 个参数调用

(我在 ASP .net 4.0 WebAPI 上)

指定路线的 /details 部分的新方法是什么?


我的核桃:

  <package id="Microsoft.AspNet.Mvc" version="4.0.20710.0" targetFramework="net40" />
  <package id="Microsoft.AspNet.Mvc.FixedDisplayModes" version="1.0.0" targetFramework="net40" />
  <package id="Microsoft.AspNet.Providers.Core" version="1.2" targetFramework="net40" />
  <package id="Microsoft.AspNet.Providers.LocalDB" version="1.1" targetFramework="net40" />
  <package id="Microsoft.AspNet.Razor" version="2.0.20715.0" targetFramework="net40" />
  <package id="Microsoft.AspNet.Web.Optimization" version="1.0.0" targetFramework="net40" />
  <package id="Microsoft.AspNet.WebApi" version="4.0.20710.0" targetFramework="net40" />
  <package id="Microsoft.AspNet.WebApi.Client" version="4.0.20710.0" targetFramework="net40" />
  <package id="Microsoft.AspNet.WebApi.Core" version="4.0.20710.0" targetFramework="net40" />
  <package id="Microsoft.AspNet.WebApi.HelpPage" version="4.0.0" targetFramework="net40" />
  <package id="Microsoft.AspNet.WebApi.OData" version="4.0.0" targetFramework="net40" />
  <package id="Microsoft.AspNet.WebApi.Tracing" version="4.0.0" targetFramework="net40" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="4.0.20710.0" targetFramework="net40" />
  <package id="Microsoft.AspNet.WebPages" version="2.0.20710.0" targetFramework="net40" />

尝试升级到 WEBAPI 的 beta 版本

update-Package : Could not install package 'Microsoft.AspNet.WebApi.Client 5.0.0-beta2'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.0', but the package does not contain any assembly references or content files
that are compatible with that framework. For more information, contact the package author.

Update-Package : Updating 'Microsoft.AspNet.Mvc 4.0.30506.0' to 'Microsoft.AspNet.Mvc 5.0.0-beta2' failed. Unable to find a version of 'Microsoft.AspNet.Mvc.FixedDisplayModes' that is compatible with 'Microsoft.AspNet.Mvc 5.0.0-beta2'.
Update-Package : Updating 'Microsoft.AspNet.Mvc 4.0.30506.0' to 'Microsoft.AspNet.Mvc 5.0.0-beta2' failed. Unable to find a version of 'Microsoft.AspNet.Mvc.FixedDisplayModes' that is compatible with 'Microsoft.AspNet.Mvc 5.0.0-beta2'.

通过重新定位到 4.5 并删除 FixedDisplayModes 包来修复这些问题

4

1 回答 1

7

您的包列表显示您正在使用 ASP.NET Web API 1。ASP.NET Web API 2 中提供了对属性路由的支持,它是Visual Studio 2013 预览版的一部分,目前处于测试版中。如果你想玩它,你有三种方法可以做到这一点:

  • 将 Package Manager 窗口顶部的下拉菜单值从“Stable only”更改为“Include prelease”,然后将您的软件包升级到Microsoft ASP.NET Web API 5.0.0-beta2(有时该Microsoft ASP.NET MVC Fixed DisplayModes软件包会阻止更新,因此您可以先尝试将其删除)
  • 将您的项目切换到ASP.NET Web API Nightly Builds
  • 下载并安装带有适当参考和新模板的 Visual Studio 2013 预览版。
于 2013-07-16T15:03:51.283 回答