[英]Utilizing Web API on client side
I have created a simple web api controller in mvc4 containing 4 methods (one for each CRUD operation). 我在mvc4中创建了一个简单的Web api控制器,其中包含4个方法(每个CRUD操作一个)。 I'm able to use fiddler to test that the methods in my controller work.
我可以使用提琴手来测试控制器中的方法是否正常工作。
I'm now trying to make a unit test to prove that these work. 我现在正在尝试进行单元测试,以证明这些功能有效。 I've managed to serialize my client side object into json format, but now how do I use this string of json to actually invoke my methods?
我已经设法将客户端对象序列化为json格式,但是现在如何使用json字符串实际调用我的方法?
If it helps, I am using Json.NET to serialize my client object - although I don't think this extention actually handles the delivery and retreival of it to the server. 如果有帮助,我正在使用Json.NET序列化我的客户端对象-尽管我认为这种扩展实际上并不能处理将其传递到服务器或从服务器撤回的过程。
Your unit tests should be written against the controller - so you don't need to make an actual HTTP request to unit test your Web API code, you just call the methods. 您应该针对控制器编写单元测试-因此,您无需发出实际的HTTP请求即可对Web API代码进行单元测试,只需调用方法即可。
From a design perspective, if you want a restful Web API, the client should be able to send a standard HTTP message without having to serialize the request. 从设计的角度来看,如果您想要一个宁静的Web API,则客户端应该能够发送标准的HTTP消息而不必序列化请求。
This is the kind of approach I have used to post an object to a restful Web API: 这是我用来将对象发布到静态Web API的一种方法:
HttpResponseMessage response;
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://url_to_service");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var responseTask = client.PostAsJsonAsync("api/resource/somethingelse", someObjectToPost).Result;
responseTask.Wait();
response = responseTask.Result;
if (response.IsSuccessStatusCode)
{
var contentTask = response.Content.ReadAsAsync<SomeResponseType>();
contentTask.Wait();
SomeResponseType responseContent = contentTask.Result;
}
else
{
//Handle error.
}
In this case, someObjectToPost
is your client-side object, though you can leave it to Web API to serialize it for you. 在这种情况下,
someObjectToPost
是您的客户端对象,尽管您可以将其留给Web API进行序列化。 In the above example I am assuming the reponse is of fictional type SomeResponseType
- you can also use ReadAsStringAsync
if the response is expected to be plain text. 在上面的例子中,我假设效应初探是虚构的类型
SomeResponseType
-你也可以使用ReadAsStringAsync
如果响应预期为纯文本。
The code presented here by nick_w is correct. nick_w在此处显示的代码是正确的。 You need to use
HttpClient
object. 您需要使用
HttpClient
对象。 And as Steve Fenton mentioned, to create unit test you don't want to do it - rather test directly against controller. 正如史蒂夫·芬顿 ( Steve Fenton)所提到的那样,不想创建单元测试,而是直接针对控制器进行测试。 But for the functional test you can do it.
但是对于功能测试,您可以做到。 I've done same thing.
我做过同样的事情。 I've created helper class so I need only to call one of Http helper methods, depending if it is GET or POST, etc. that I do.
我已经创建了helper类,因此我只需要调用Http helper方法之一即可,这取决于我执行的是GET还是POST等。 This helper uses generic types so it operates with any types that being passed.
该帮助程序使用通用类型,因此可以与传递的任何类型一起使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.