Asp.net web api RouteAttribute不见了

Asp.net web api RouteAttribute不见了,asp.net-web-api,attributerouting,routeattribute,Asp.net Web Api,Attributerouting,Routeattribute,我正在Web应用程序中使用Microsoft ASP.NET Web API 2.2 控制器如下所示: public class EventsController: ApiController { [HttpGet] [Route("GetAllActivities")] public IEnumerable<IActivity> GetEvents() { /* stuff */ } } Install-Package AttributeRouting

我正在Web应用程序中使用Microsoft ASP.NET Web API 2.2

控制器如下所示:

public class EventsController: ApiController
{
  [HttpGet]
  [Route("GetAllActivities")]
  public IEnumerable<IActivity> GetEvents()
  {
    /* stuff */
  }
}
Install-Package AttributeRouting
公共类事件控制器:ApiController
{
[HttpGet]
[路线(“GetAllActivities”)]
公共IEnumerable GetEvents()
{
/*东西*/
}
}
问题描述 我们正试图将RouteAttribute类包含在我们的项目中,但我们发现很难做到这一点。有时,我们认为我们得到了它,但当我们删除bin目录并尝试重新构建时,它不在那里,单词Route被涂成红色。另一个症状是GlobalConfiguration.Configure和HttpConfiguration.MapHttpAttribute路由将丢失。我们也无法理解擦除bin目录和从头开始构建的区别。甚至很难理解基于属性的路由如何不是WebAPI 2.2的一部分,而是一个附加组件。如果我们做对了,有些软件包是强制性的,有些软件包是不允许工作的。这是真的吗

这是我们尝试过的,没有特殊顺序:

  • 安装包属性例程
  • 安装包AttributeRouting.WebApi
  • 安装软件包Microsoft.AspNet.WebApi.WebHost
  • 更新包-重新安装Microsoft.AspNet.WebApi.WebHost
  • 更新包-重新安装
  • 卸载包属性例程
我让所有这些都工作了一次,然后一位同事在从我们的存储库中检出代码时找不到RouteAttribute类,没有忽略一个文件。然后,我删除了bin目录并重建了我的版本,然后我退回到找不到RouteAttribute类的状态

我们通过更新确保了我们拥有相同版本的VisualStudio,但问题仍然存在

我知道MVC也有路由属性,它们与webapi路由属性不同。我还知道webapi路由属性分为web托管和自托管类型。我正在尝试在我的参考列表中引用Ssytem.Web.Http.WebHost,因为我们在IIS上托管它

不幸的是,他们做出了这样的决定:对于所有这些应用程序,类的名称保持不变。如果它们不同,就不会发生误解

以下是项目中的参考列表,来自csproj文件

<Reference Include="AttributeRouting, Version=3.5.6.0, Culture=neutral, PublicKeyToken=c10f85d521a011a9, processorArchitecture=MSIL">
    <HintPath>..\..\packages\AttributeRouting.Core.3.5.6\lib\net40\AttributeRouting.dll</HintPath>
    <Private>True</Private>
</Reference>
<Reference Include="AttributeRouting.Web, Version=3.5.6.0, Culture=neutral, PublicKeyToken=c10f85d521a011a9, processorArchitecture=MSIL">
    <HintPath>..\..\packages\AttributeRouting.Core.Web.3.5.6\lib\net40\AttributeRouting.Web.dll</HintPath>
    <Private>True</Private>
</Reference>
<Reference Include="AttributeRouting.Web.Http, Version=3.5.6.0, Culture=neutral, PublicKeyToken=c10f85d521a011a9, processorArchitecture=MSIL">
    <HintPath>..\..\packages\AttributeRouting.Core.Http.3.5.6\lib\net40\AttributeRouting.Web.Http.dll</HintPath>
    <Private>True</Private>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
    <HintPath>..\..\packages\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll</HintPath>
    <Private>True</Private>
</Reference>
<Reference Include="System.Net.Http" />
<Reference Include="System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
    <HintPath>..\..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll</HintPath>
    <Private>True</Private>
</Reference>
<Reference Include="System.Net.Http.WebRequest" />
<Reference Include="System.Web.DynamicData" />
<Reference Include="System.Web.Entity" />
<Reference Include="System.Web.ApplicationServices" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Core" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Web.Extensions" />
<Reference Include="System.Web.Http, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
    <HintPath>..\..\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll</HintPath>
    <Private>True</Private>
</Reference>
<Reference Include="System.Web.Http.WebHost, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
    <HintPath>..\..\packages\Microsoft.AspNet.WebApi.WebHost.5.2.3\lib\net45\System.Web.Http.WebHost.dll</HintPath>
    <Private>True</Private>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Drawing" />
<Reference Include="System.Web" />
<Reference Include="System.Xml" />
<Reference Include="System.Configuration" />
<Reference Include="System.Web.Services" />
<Reference Include="System.EnterpriseServices" />
<Reference Include="WebActivator, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
    <HintPath>..\..\packages\WebActivator.1.0.0.0\lib\WebActivator.dll</HintPath>
    <Private>True</Private>
</Reference>

..\..\packages\AttributeRouting.Core.3.5.6\lib\net40\AttributeRouting.dll
真的
..\..\packages\AttributeRouting.Core.Web.3.5.6\lib\net40\AttributeRouting.Web.dll
真的
..\..\packages\AttributeRouting.Core.Http.3.5.6\lib\net40\AttributeRouting.Web.Http.dll
真的
..\..\packages\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll
真的
..\..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll
真的
..\..\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll
真的
..\..\packages\Microsoft.AspNet.WebApi.WebHost.5.2.3\lib\net45\System.Web.Http.WebHost.dll
真的
..\..\packages\WebActivator.1.0.0.0\lib\WebActivator.dll
真的

您有多个问题,我将尝试全部回答

属性路由

属性路由是WebAPI 2.2的标准功能。您可以通过使用
RouteAttribute
装饰您的方法来使用它。此属性随软件包
Microsoft.AspNet.WebApi.WebHost
提供

NuGet包
AttributeRouting
是另一个默认情况下未包含在WebAPI项目中的第三方包。如果要将其添加到项目中,可以按如下方式安装:

public class EventsController: ApiController
{
  [HttpGet]
  [Route("GetAllActivities")]
  public IEnumerable<IActivity> GetEvents()
  {
    /* stuff */
  }
}
Install-Package AttributeRouting
我的猜测是,你可能不需要这个包裹

接下来,

NuGet软件包是可以添加到解决方案中的第三方模块。添加的包存储在名为“包”的解决方案文件夹中(这就是为什么项目文件包含
HintPath
。\..\packages\*
。解决方案中任何项目使用的所有包都存储在该文件夹中。单个项目使用的包在项目级文件
packages.config
中引用

现在,当同事从源代码管理系统(如TFS)检索您的解决方案并构建您的解决方案时,应该自动检索(从NuGet)包并且解决方案应该可以工作。这是Visual Studio的默认行为。但是,可以选择退出自动检索。如果您或您的同事选择退出,则不会检索包,并且由于缺少引用,解决方案也不会生成

然后,bin文件夹

bin文件夹是Visual Studio将在其中放置已编译的解决方案的文件夹。这意味着,每次生成解决方案时,VS都会清理该文件夹,将源代码编译为程序集,并将已编译的程序集以及引用的程序集放在bin文件夹中

例如,当新同事从源代码管理检索到您的解决方案时,他将没有bin文件夹。他构建解决方案后,bin文件夹将被创建,并填充构建程序集

最后,修复您的解决方案

要修复原始问题(
Route
无法识别),请检查是否自动检索NuGet软件包。选项

  • 允许NuGet下载丢失的软件包
  • 在Visual Studio中生成期间自动检查缺少的包
都应选中。这两个选项都可以在Visual Studio选项中的Package Manager常规设置中找到

之后,您必须将WebAPI包添加到解决方案中:

Install-Package Microsoft.AspNet.WebApi
这还将安装一些依赖项(Microsoft.AspNet.WebApi.Client、Microsoft.AspNet.WebApi.Core、Microsoft.AspNet.WebApi.WebHost和Newtonsfot.Json)以及之后的
Route
GlobalConfiguration.Configure