我正在学习本教程:
并查看“获取书籍详细信息”部分。
我看到了这段代码:
[HttpGet("api/books/{id}/details")]
public BookDetailDto GetBookDetail(int id)
{
}
然而我收到错误
HttpGet有0个参数但是用1个参数调用
(我在ASP .net 4.0 WebAPI上)
指定路线的/详细信息部分的新方法是什么?
我的nuget:
<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的测试版
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包
来修复这些问题答案 0 :(得分:7)
您的包列表显示您正在使用ASP.NET Web API 1. ASP.NET Web API 2中提供了对属性路由的支持,它是Visual Studio 2013 Preview的一部分测试版。如果你想玩它,你有三种方法可以做到这一点:
Microsoft ASP.NET Web API 5.0.0-beta2
(有时Microsoft ASP.NET MVC Fixed DisplayModes
软件包阻止更新,因此您可能会尝试先删除它)