c# webapi httpget attribute

本文关键字:attribute httpget webapi | 更新日期: 2023-09-27 18:22:37

我正在学习本教程:

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上)

指定路线详细信息部分的新方法是什么?


我的裸体:

  <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包

c# webapi httpget attribute

修复了这些问题

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

  • 将Package Manager窗口顶部的下拉列表值从"仅稳定"更改为"包括预租赁",并将您的程序包升级为Microsoft ASP.NET Web API 5.0.0-beta2(有时Microsoft ASP.NET MVC Fixed DisplayModes程序包阻止更新,因此您可能会尝试先将其删除)
  • 将您的项目切换到ASP.NET Web API Nightly Build
  • 下载并安装Visual Studio 2013预览版,该预览版附带适当的引用和新模板