I am using Microsoft ASP.NET Web API 2.2 in a web application.
A controller looks like this:
public class EventsController: ApiController
{
[HttpGet]
[Route("GetAllActivities")]
public IEnumerable<IActivity> GetEvents()
{
/* stuff */
}
}
Problem description We are trying to have the RouteAttribute class included in our project and we found that it is hard to do so. Sometimes, we think we got it, but when we remove the bin directory and try to build again, its not there and the word Route is colored red. Another symptom is that GlobalConfiguration.Configure and HttpConfiguration.MapHttpAttributeRoutes will be missing. We also fail to understand the difference it makes to wipe away the bin directory and build from scratch. It is even hard to understand how attribute based routing is not already a part of WebAPI 2.2, but an add-on. If we get it right, some packages are mandatory and some packages are not allowed to get this working. Is that true?
This is what we have tried, in no special order:
- Install-Package AttributeRouting
- Install-Package AttributeRouting.WebApi
- Install-Package Microsoft.AspNet.WebApi.WebHost
- Update-Package -reinstall Microsoft.AspNet.WebApi.WebHost
- Update-Package -reinstall
- Uninstall-Package AttributeRouting
I had all this working once, then a colleague could not find the RouteAttribute class when checking out the code from our repository, not ignoring a single file. Then I removed the bin directory and rebuilt my version and I regressed to not being able to find the RouteAttribute class.
We ensured that we had the same version of visual studio by updating, but the problems remains.
I am aware of MVC having route attributes as well and that they are not the same thing as webapi route attributes. I am also aware of webapi route attributes are split up in web hosted and self hosted kinds. I am trying to have a reference to Ssytem.Web.Http.WebHost in my reference list, because we are hosting this on an IIS.
Unfortunately, they made that decision to keep the name of the classes the same for all those applications. If they were different, misunderstandings would not occur.
Here is the list of references in the project, from the csproj file.
<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>