Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# webapi httpget attribute

I am following this tutorial:

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

and looking at the "Get Book Details" section.

I see this code:

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

however I am getting errors that

HttpGet has 0 parameters but is invoked with 1 argument

(i'm on ASP .net 4.0 WebAPI)

What is the new way of specifying the /details part of the route?


my 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" />

trying to upgrade to the beta version of 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'.

fixed these by retargeting to 4.5 and removing the FixedDisplayModes package

like image 742
GreyCloud Avatar asked Mar 24 '23 04:03

GreyCloud


1 Answers

Your packages list shows that you are using ASP.NET Web API 1. The support for Attribute Routing is available in ASP.NET Web API 2 which is part of Visual Studio 2013 Preview and curently in beta version. If you want play with it you have three ways to do this:

  • Change the dropdown at the top of Package Manager window value from "Stable only" to "Include prelease" and upgrade your package to Microsoft ASP.NET Web API 5.0.0-beta2 (sometimes the Microsoft ASP.NET MVC Fixed DisplayModes package is preventing the update so you might try to remove it first)
  • Switch your project to ASP.NET Web API Nightly Builds
  • Download and install Visual Studio 2013 Preview which comes with proper referencs and new templates.
like image 114
tpeczek Avatar answered Apr 19 '23 08:04

tpeczek