c# - routing and integration testing webapi via selfhosting -
i using code:
[fact] public void valuecontroller_withgetmethos_shouldreturnvaliddata_nobaseclass() { var configuration = new httpselfhostconfiguration("http://localhost:64466"); configuration.includeerrordetailpolicy = includeerrordetailpolicy.always; configuration.services.replace(typeof(iassembliesresolver), new webapiclassbase.testassemblyresolver(typeof(valuescontroller))); configuration.routes.maphttproute( name: "defaultapi", routetemplate: "api/{controller}/{id}", defaults: new { id = routeparameter.optional } ); var server = new httpselfhostserver(configuration); try { server.openasync().wait(); var request = new httprequestmessage { requesturi = new uri("http://localhost:64466/api/values"), method = httpmethod.get }; var client = new httpclient(server); using (httpresponsemessage response = client.sendasync(request).result) { response.should().not.be.null(); response.issuccessstatuscode.should().be.true(); string[] result = response.content.readasasync<string[]>().result; result.length.should().be.equalto(4); result[0].should().be.equalto("http://tostring.it"); result[1].should().be.equalto("http://imperugo.tostring.it"); result[2].should().be.equalto("http://twitter.com/imperugo"); result[3].should().be.equalto("http://www.linkedin.com/in/imperugo"); } } { configuration.dispose(); server.dispose(); } }
inspired this, contains relevant valuescontroller. have noticed had adapt routing:
configuration.routes.maphttproute( name: "defaultapi", routetemplate: "api/{controller}/{id}", defaults: new { id = routeparameter.optional } );
according 1 in webapiconfig. however, when try access action attribute routing this:
route("calculator/addtwonumbers/a/{a}/b/{a}")] public ihttpactionresult addtwonumbers(payload payload)
i 404 - not found error. have define declarative routing? if so, integration testing seems quite tedious. feedback appreciated. thanks.
ps:
i tried:
configuration.maphttpattributeroutes();
which not seem help
you need setup attributerouting before default route configured.
configuration.maphttpattributeroutes(); configuration.routes.maphttproute( name: "defaultapi", routetemplate: "api/{controller}/{id}", defaults: new { id = routeparameter.optional } );
routes evaluated in order added routecollection
. if default route comes first, match incoming routes before attribute routes have chance matched.
Comments
Post a Comment