Search code examples
c#asp.netasp.net-mvc-4asp.net-web-apiurl-routing

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


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


Solution

  • 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;