Url.Link无法在WebAPI中使用
使用下面的unit testing..我试图测试我的webapi。
[Test] public void CheckControllerForCreate() { var config = new HttpConfiguration(); config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always; var request = new HttpRequestMessage(HttpMethod.Post, "https://localhost/api/product"); var route = config.Routes.MapHttpRoute("Foo", "api/{controller}/{id}"); var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "products" } }); var controller = new ProductController { ControllerContext = new HttpControllerContext(config, routeData, request), Request = request, Url = new UrlHelper(request) }; controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config; var result = controller.Create(new Product {Id = 4, Name = "Tomato Soup", Category = "Groceries", Price = 1}); } [HttpPost] public HttpResponseMessage Create(Product product) { var url = Url; if (product == null) throw new HttpResponseException(new HttpResponseMessage{StatusCode = HttpStatusCode.BadRequest,ReasonPhrase = "Product is not specified"}); products.Add(product); var response = Request.CreateResponse(HttpStatusCode.Created, product); string uri = Url.Link("Foo", product.Id); response.Headers.Location = new Uri(uri); return response; }
Create Action抛出exception,因为uri
为null
。 现在,Url助手正在正确地拾取RouteName,否则会出现RouteName not foundexception。 我假设我的配置有些错误。
我提到了https://www.peterprovost.org/blog/2012/06/16/unit-testing-asp-dot-net-web-api以及其他一些用于对控制器进行unit testing的post。
WebAPI方法在这里是codeplex
https://aspnetwebstack.codeplex.com/SourceControl/changeset/view/1acb241299a8#src/System.Web.Http/Routing/UrlHelper.cs
编辑
我把它缩小到vpd为null(UrlHelper)
IHttpVirtualPathData vpd = configuration.Routes.GetVirtualPath( request: request, name: routeName, values: routeValues);
似乎无法弄清楚为什么?
您需要将routeData设置为请求,方法与配置相同:
controller.Request.Properties[HttpPropertyKeys.HttpRouteDataKey] = routeData;
您也错误地使用了Url.Link
帮助程序。 您尚未指定控制器,也未指明ID。
控制器中的代码应如下所示:
string uri = Url.Link("Foo", new { id = product.Id, controller = "product" });
更新:
这是一个完整的例子。
控制器:
public class ProductController : ApiController { public HttpResponseMessage Create(int id) { var uri = Url.Link("Foo", new { id = id, controller = "product" }); return Request.CreateResponse(HttpStatusCode.OK, uri); } }
测试:
上述就是C#学习教程:Url.Link无法在WebAPI中使用分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
// arrange var config = new HttpConfiguration(); config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always; var request = new HttpRequestMessage(HttpMethod.Post, "https://localhost/api/product"); var route = config.Routes.MapHttpRoute("Foo", "api/{controller}/{id}"); var routeData = new HttpRouteData(route, new HttpRouteValueDictionary(new { controller = "product" })); var controller = new ProductController { ControllerContext = new HttpControllerContext(config, routeData, request), Request = request, Url = new UrlHelper(request) }; controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config; controller.Request.Properties[HttpPropertyKeys.HttpRouteDataKey] = routeData; // act var result = controller.Create(4); // assert ...
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/951965.html