AttributeRouting funktioniert nicht mit dem HttpConfiguration-Objekt zum Schreiben von Integrationstests

Ich erstelle einige Integrationstests gemäß den hier beschriebenen Ideen:http://www.strathweb.com/2012/06/asp-net-web-api-integration-testing-with-in-memory-hosting/

Wenn ich versuche, Routen von einem handgefertigten HttpConfiguration-Objekt zu registrieren, wird die folgende Fehlermeldung angezeigt: "Der Einschränkungseintrag 'inboundHttpMethod' für die Route mit der Routenvorlage 'api / Contacts / {id}' muss einen Zeichenfolgewert haben oder von sein ein Typ, der 'IHttpRouteConstraint' implementiert. "

Beispielcode: Controller:

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

Testklasse (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, string mthv, 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);
            }
        }
    }

Der Fehler tritt in der Zeile "client.SendAsync" auf. Ich habe config.Routes überprüft und der Datentyp für die "Einschränkungen" für "inboundHttpMethod" lautet "AttributeRouting.Web.Http.WebHost.Constraints.InboundHttpMethodConstraint". Es wird anscheinend ein Zeichenfolgenwert erwartet. Jede Hilfe wäre sehr dankbar.

Antworten auf die Frage(1)

Ihre Antwort auf die Frage