“找不到类型或名称空间名称'Route'";使用“属性路由" [英] "The type or namespace name 'Route' could not be found" using "attribute routing"

查看:267
本文介绍了“找不到类型或名称空间名称'Route'";使用“属性路由"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是尝试将一些代码从一个工作项目拼接到另一个项目. "from"项目使用属性路由",您可以在其中将[Route(…)]指令嵌入Web API控制器模块中,以指示应将哪些HTTP消息路由到哪个服务例程.

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.

在"from"项目中工作正常,但是在"to"项目中,我收到构建错误找不到类型或名称空间名称"Route"(您是否缺少using指令或程序集引用?) "

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

我已经尝试将所有using语句从"from"项目复制到"to"项目,但这并没有明显的效果. MS的文档都没有建议使用NuGet包(甚至是using语句).据推测,这两个项目都是ASP.NET MVC 4.

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.

(是的,我用config.MapHttpAttributeRoutes();语句更新了WebApiConfig.cs.)

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

任何想法?

推荐答案

属性路由在ASP.NET MVC 5或更高版本以及ASP.NET Web API 2中是本机的.

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

但是,有一个项目,该项目允许在ASP.NET MVC和Web API的早期版本中使用属性路由.您应该仔细阅读此页面.

However, there is a project that allows to use attribute routing it in previous version of ASP.NET MVC,and Web API. You should read this page carefully.

如您在链接页面中所见,该项目以NuGet软件包的形式提供,因此可以这样安装:

As you can see in the linked page, this project is available as a NuGet package, so that it can be installed like this:

  • Install-Package AttributeRouting(对于MVC)
  • Install-Package AttributeRouting.WebApi(用于Web API)
  • Install-Package AttributeRouting.WebApi.Hosted(用于自托管的Web API)
  • Install-Package AttributeRouting (for MVC)
  • Install-Package AttributeRouting.WebApi (for Web API)
  • Install-Package AttributeRouting.WebApi.Hosted (for self-hosted Web API)

请注意,每个版本以及MVC和Web API的属性路由的名称空间都不同.因此,您必须浏览已安装程序包所包含的.dll,以找到正确的名称空间,并相应地更改using.例如:

Please, be aware that the namespaces of attribute routing are different for each version and for MVC and Web API. So, you must 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;

这篇关于“找不到类型或名称空间名称'Route'";使用“属性路由"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆