Another entry in the series “tweets and blog posts I write mostly to have somewhere to look the next time I encounter this error”: this time, I spent too much time figuring out why this piece of code in an MVC view:
@Url.HttpRouteUrl("GetPerformance", new { id = Model.PerformanceId })
combined with this (working, I might add) API endpoint
[RoutePrefix("api/performances")]
public class PerformancesController : ApiController
{
[HttpGet]
[Route("", Name="GetPerformance")
public Performance GetPerformances(string id = "")
{
// Some code...
}
}
did not resolve to /api/controllers/{id}, but instead presented me with a glorious yellow screen proudly presenting and ArgumentException with the exception message
A route named ‘GetPerformance’ could not be found in the route collection.
Parameter name: name
As if often the case, it turns out I had tried to be a little too clever: This was an episerver project with the legacy global.asax.cs file doing the MVC routing, while the web api was set up in the owin startup.cs class, with a new HttpConfiguration instance created there and attached with app.UseWebApi.
To resolve the error, I had to tie the Web API registration to GlobalConfiguration.Configuration instead of a new instance in startup.cs. With that done, both MVC and API routing were aware of each other, the error went away, and I was able to programmatically create web API route links in MVC views.