简体   繁体   English

在客户端使用Web API

[英]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.

相关问题 ASP Net Core Web API:不支持客户端 GroupBy - ASP Net Core Web API: Client side GroupBy is not supported 客户端的ASPNET Web API应用程序返回错误 - ASPNET Web API application return error in client side 将 Blazor 客户端消费 Web API 与 Windows 身份验证结合使用 - Using Blazor Client-Side consuming Web API with Windows Authentication 如何验证ASP.NET MVC Web API是否使用SSL - How to verify asp.net mvc web API is utilizing SSL 在客户端从Web打印 - Printing from the web at client side 使用Web API的客户端Web应用程序,如何根据服务器端Web API的期望填充选择框字段值? - Client-side web app consuming Web API, how to populate select box field values based on expectations of server-side Web API? 通过.NET Web API 2从客户端进行身份验证-纯文本可见的凭据 - Authentication via .net web api 2 from client side - plain text visible credentials asp.net core web api无法从客户端发送对象 - asp.net core web api cant send object from client side 如何从ASP.NET Core的客户端调用Web api方法? - How to invoke web api method from client side in asp.net core? 需要使用从Web api获取的DataTable并将其显示在aspx页面的客户端 - need to consume DataTable getting from a web api and show it at the client side in an aspx page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM