Assume RouteData is empty instead of returning null in HttpRoute.GetVirtualPath.
When trying to test link generation inside ApiController, the route data needs to be set up properly, which involves the following steps:
```
IHttpRoute route = controller.Configuration.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
IHttpRouteData routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "products" } });
controller.Request.Properties[HttpPropertyKeys.HttpRouteDataKey] = routeData;
```
When the user is generating all the links using Url.Link and providing all the required information, there is no need for RouteData. For example:
```
new Uri(Url.Link("api", new { controller = "Products", id = value.Id }));
```
If the user doesn't set the RouteData in the test, Url.LInk returns null because of the following check in the GetVirtualPath of the HttpRoute class:
```
IHttpRouteData routeData = request.GetRouteData();
if (routeData == null)
{
return null;
}
```
The current implementation requires the user know about RouteData, which is an advanced concept that the average Web API user doesn't need to understand.
When trying to test link generation inside ApiController, the route data needs to be set up properly, which involves the following steps:
```
IHttpRoute route = controller.Configuration.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
IHttpRouteData routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "products" } });
controller.Request.Properties[HttpPropertyKeys.HttpRouteDataKey] = routeData;
```
When the user is generating all the links using Url.Link and providing all the required information, there is no need for RouteData. For example:
```
new Uri(Url.Link("api", new { controller = "Products", id = value.Id }));
```
If the user doesn't set the RouteData in the test, Url.LInk returns null because of the following check in the GetVirtualPath of the HttpRoute class:
```
IHttpRouteData routeData = request.GetRouteData();
if (routeData == null)
{
return null;
}
```
The current implementation requires the user know about RouteData, which is an advanced concept that the average Web API user doesn't need to understand.