asp.net-web-api – AttributeRouting不能与HttpConfiguration对象一起编写集成测试

我按照这里概述的想法创建了一些集成测试:
http://www.strathweb.com/2012/06/asp-net-web-api-integration-testing-with-in-memory-hosting/

当我尝试从手工制作的HttpConfiguration对象注册路由时,我收到以下错误
“路由模板’api / Contacts / {id}’的路由上的约束条目’inboundHttpMethod’必须具有字符串值或者是实现’IHttpRouteConstraint’的类型.”

示例代码
控制器:

[RoutePrefix("api")]
    public class ContactsController : ApiController
    {
        [GET("Contacts/{id}",RouteName="GetContactsById")]
        public ContactDTO Get(int id)
        {
      return new ContactDTO{ ID =1,Name="test"};
        }
    }
}

TestClass(MSTest):

[TestClass]
    public class ContactsTest
    {
        private string _url = "http://myhost/api/";
        private static HttpConfiguration config = null;
        private static HttpServer server = null;
        private HttpRequestMessage createRequest(string url,string mthv,HttpMethod method)
        {
             var request = new HttpRequestMessage();
            request.RequestUri = new Uri(_url + url);
            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(mthv));
            request.Method = method;
            return request;
        }
        private HttpRequestMessage createRequest<T>(string url,HttpMethod method,T content,MediaTypeFormatter formatter) where T : class
        {
            HttpRequestMessage request = createRequest(url,mthv,method);
            request.Content = new ObjectContent<T>(content,formatter);

            return request;
        }

        [ClassInitializeAttribute]
        public static void ClassInitialize(TestContext ctx)
        {
            config = new HttpConfiguration();
            config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
            config.Services.Replace(
                typeof(IDocumentationProvider),new DocProvider());

            config.Services.Replace(
                typeof(IApiExplorer),new VersionedApiExplorer(config));

            config.Services.Replace(
                typeof(IHttpControllerSelector),new VersionHeaderVersionedControllerSelector
                    (config)
                    );
            AttributeRoutingHttpConfig.RegisterRoutes(config.Routes);
            WebApiConfig.Register(config);
            server = new HttpServer(config);
        }

        public static void ClassCleanup()
        {
            config.dispose();
            server.dispose();
        }

        [TestMethod]
        public void RetrieveContact()
        {
            var request = createRequest("Contacts/12","application/json",HttpMethod.Get);
            var client = new HttpClient(server);

            using (HttpResponseMessage response = client.SendAsync(request).Result)
            {
                Assert.IsNotNull(response.Content);
            }
        }
    }

错误发生在“client.SendAsync”行上.我检查了config.Routes和”inboundHttpMethod’的“Constraints”的数据类型是AttributeRouting.Web.Http.WebHost.Constraints.InboundHttpMethodConstraint
看来字符串值是预期的.
任何帮助将非常感激.

解决方法

有同样的问题.在这里找到答案:

https://github.com/mccalltd/AttributeRouting/issues/191

你需要更换

AttributeRoutingHttpConfig.RegisterRoutes(config.Routes);

config.Routes.MapHttpAttributeRoutes(cfg =>
{
   cfg.InMemory = true;
   cfg.AutoGenerateRouteNames = true;
   cfg.AddRoutesFromAssemblyOf<ContactsController>();// Or some other reference...
});

我发现我也需要AutoGenerateRouteNames部分.

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....