"The type or namespace name 'Route' could not be found" using "attribute routing"

50,881

Solution 1

Attribute Routing is native in ASP.NET MVC 5, or later, and ASP.NET Web API 2.

For previous versions you can use one of the packages from the AttributeRouting project that allows to use attribute routing in previous version of ASP.NET MVC, and Web API. The availabe nuget packages are:

  • Install-Package AttributeRouting (for MVC)
  • Install-Package AttributeRouting.WebApi (for Web API)
  • Install-Package AttributeRouting.WebApi.Hosted (for self-hosted Web API)

Please, be aware that the namespaces of attribute routing are different for each version, so you must review the project soruces code, or browse the .dll included by the installed package, to find out the right namespace, and change your using accordingly. For example:

using AttributeRouting.Web.Http;

Solution 2

This comment from Vedran Mandić solved the problem for me. I'm re-posting it here because I think it should be the answer (or at least a answer).

I did an 'Update-Package Microsoft.AspNet.WebApi.WebHost -reinstall' and it worked. Funny this happens after getting the latest version on different PCs from TFS. I guess this happens because of nuget packages not working properly with the versioning system

Solution 3

In my case, in web api project there were two using references:

using System.Web.Http;
using System.Web.Mvc; 

As soon as I removed System.Web.Mvc, the error was gone.

Share:
50,881
Hot Licks
Author by

Hot Licks

Older than dirt. I got t-shirts older than you, kiddo.

Updated on July 16, 2022

Comments

  • Hot Licks
    Hot Licks almost 2 years

    Just trying to splice some code from one working project to another. The "from" project uses "attribute routing" where you embed [Route(…)] directives in the Web API controller modules to indicate what HTTP message should route to what service routine.

    Works fine in the "from" project, but in the "to" project I get the build error "The type or namespace name 'Route' could not be found (are you missing a using directive or an assembly reference?)"

    I've tried copying essentially all of the using statements from the "from" project to the "to" project, but that has no apparent effect. None of the MS documentation suggests that a NuGet package is required (or even a using statement). Both projects are supposedly ASP.NET MVC 4.

    (And, yes, I updated WebApiConfig.cs with the config.MapHttpAttributeRoutes(); statement.)

    Any ideas??

  • Hot Licks
    Hot Licks almost 10 years
    Unfortunately, installing the AttributeRouting package does not make the messages go away. (I have a "using System.Web.Http.Routing;" in the module.)
  • JotaBe
    JotaBe almost 10 years
    That's logical. The namespace is different. You must get sure which version of Web API are using. The attributes to use depend on versions (of MVC and Web API ) and if it's Web API, self hosted Web API or MVC.
  • Leandro Bardelli
    Leandro Bardelli over 8 years
    this rocks. Same problem here with TFS and nuget
  • Ads
    Ads almost 8 years
    Thanks so much for the tip, but I personally hate this kind of fix. It usually involves lots of head banging, wasted time and frustration.
  • sal
    sal about 7 years
    Still getting error. [RoutePrefix("xxx")] taking referance but still [Route("xxx")] unavalible.
  • Thom Parkin
    Thom Parkin almost 7 years
    Me too. UNTIL I followed the advice from ClearCloud8 (above)
  • Coyttl
    Coyttl over 3 years
    Oddly - updated VS2019 from 16.7.5 to 16.8.2 (latest) and had to do this. Worked like a charm
  • Dominik S
    Dominik S about 3 years
    Thel link is dead, so I provide link to GH: github.com/mccalltd/AttributeRouting