2

我创建了一个 Web API 操作,如下所示

[HttpPost]
public void Load(string siteName, string providerName, UserDetails userDetails)
{
// implementation
}

我为此注册的路线如下(不确定是否正确?):

 config.Routes.MapHttpRoute(
             name: "loadUserDetails",
             routeTemplate: "sites/{siteName}/User/Load/{providerName}/{userDetailsList}",
             defaults: new
             {
                 controller = "User",
                 action = "Load",
                 providerName = UrlParameter.Optional
             });

providerName 参数应该是可选的,我正在使用 Xml 序列化。

该操作应响应以下 url 示例:

http://www.domain.com/sites/site1/user/load/provider1/[some user details in the post]
or
http://www.domain.com/sites/site1/user/load/[some user details in the post]

我怎样才能对此操作进行后期调用,以便我可以测试我的服务?

4

3 回答 3

3

来自:带有 post 的 HTTP 请求

HttpWebRequest request =
    (HttpWebRequest)WebRequest.Create(@"http:\\domain.com\page.asp");

ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "username=user";
postData += "&password=pass";
byte[] data = encoding.GetBytes(postData);

request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;

using (Stream stream = request.GetRequestStream())
{
    stream.Write(data,0,data.Length);
}

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
于 2013-04-18T15:27:06.187 回答
0

如果您的问题是如何测试您的操作,您可以使用Fidler

http://www.mehdi-khalili.com/fiddler-in-action/part-2

于 2013-04-18T15:26:00.570 回答
0

使用 RestSharp 和 Web 请求查看来自此处的响应:

http://pooyakhamooshi.blogspot.co.uk/2013/04/how-to-post-request-to-web-api-web.html

于 2013-04-19T09:22:40.900 回答