Url.链接在WebAPI中不起作用

本文关键字:不起作用 WebAPI 链接 Url | 更新日期: 2023-09-27 18:00:27

使用下面的单元测试。。我正在尝试测试我的webapi。

[Test]
public void CheckControllerForCreate()
{
    var config = new HttpConfiguration();
    config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
    var request = new HttpRequestMessage(HttpMethod.Post, "http://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;
}

创建操作引发异常,因为urinull。现在,Url帮助程序正在正确地提取RouteName,否则将出现RouteName未找到异常。我认为我的配置有问题。

我提到http://www.peterprovost.org/blog/2012/06/16/unit-testing-asp-dot-net-web-api以及用于对控制器进行单元测试的其他几个岗位。

WebAPI方法在codeplex 上

http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/1acb241299a8#src/System.Web.Http/Routing/UrlHelper.cs

编辑

我已经将其缩小到vpd在(UrlHelper)中为空

IHttpVirtualPathData vpd = configuration.Routes.GetVirtualPath(
                request: request,
                name: routeName,
                values: routeValues);

似乎不明白为什么?

Url.链接在WebAPI中不起作用

您需要将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);
    }
}

测试:

// arrange
var config = new HttpConfiguration();
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
var request = new HttpRequestMessage(HttpMethod.Post, "http://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
...