c# - How to test a WebApi service? -


i'm new webapi , i've been reading information don't know how start application.

i had project many wfc services .net 3.5. so, updated project 4.5.1. created controller visual studio 2012 wizard. then, when controller created, see class template get, post, put, delete methods. place post method , want test service httpclient.

i tried apply solution in green following forum: how post xml value web api?

i'm gonna receive xml string structure of contract model.

i run project visual studio development server.

but have troubles url test service.

i saw many pages people http://localhost:port/api/contract. don't still know how works. how can test service? path or url test service?

webapi, mvc, based on routing. default route /api/{controller}/{id} (which could, of course, altered). found in ~/app_start/webapiconfig.cs file of new project, given you're migrating don't have likely. so, wire can modify application_start include:

globalconfiguration.configure(webapiconfig.register); 

then, define class:

public static class webapiconfig {     public static void register(httpconfiguration config)     {         // web api configuration , services          // web api routes         config.maphttpattributeroutes();          config.routes.maphttproute(             name: "defaultapi",             routetemplate: "api/{controller}/{id}",             defaults: new { id = routeparameter.optional }         );     } } 

if created testcontroller controller , wanted make post request instance running locally, you'd access http://localhost:12345/api/test/ (with appropriate verb). 12345 local port visual studio using host service (and can found viewing project's properties, navigating "web" tab).

having said that, testing best performed on the project (without making external call). there several posts on subject, come down following:

[testmethod] public void should_return_single_product() {     // arrange     var repo = new fakerepository<widget>();     var controller = new widgetcontroller(repo);     var expected = repo.find(1);     // act    var actual = controller.getwidget(1) oknegotiatedcontentresult<widget>;     // assert    assert.isnotnull(actual);    assert.areequal(expected.id, actual.content.id); } 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -