测试我的post api方法

时间:2015-03-01 11:49:25

标签: c# asp.net asp.net-web-api

我有一个api项目,其post方法如下:

[Route("api/PostReviewedStudyData")]
[HttpPost]
public bool PostReviewedStudyData(string jsonObject)
{
    //some stuff
    return true;
}

我似乎无法测试它我已经编写了这样的测试方法:

HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(@"http://localhost:60604/api/PostReviewedStudyData");

ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "Some json parsed object";
byte[] data = encoding.GetBytes(postData);

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

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

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

string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

我收到404错误。我的Api方法写得不好,还是我的测试方法呢?

1 个答案:

答案 0 :(得分:1)

有几件事:同时更改签名: