我已经将我的 web api 项目升级到最新版本,使用 MVC 5 应用程序运行正常,但是这行代码在我的单元测试中不再工作:
string uri = this.Url.Link("DefaultApi", new { id = savedOrganization.Id });
控制器的 Url 属性现在为空。这就是我配置模拟控制器的方式:
var config = new HttpConfiguration();
var request = new HttpRequestMessage(HttpMethod.Post, "http://xxx/api/organization");
var route = config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}");
var routeData = new HttpRouteData(route, new HttpRouteValueDictionary {{"controller", "organization"}});
controller.ControllerContext = new HttpControllerContext(config, routeData, request);
controller.Request = request;
controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;
controller.Request.Properties[HttpPropertyKeys.HttpRouteDataKey] = routeData;
在升级到 MVC 5 之前,它运行良好。
当我调试测试时,它显示 Url 属性现在为空