我无法弄清楚如何将字符串数组发布到我的APIController
我希望能够发送以下JSON字符串:
{"userName":"un","userPassword":"password"}
这是我的控制器:
public class CheckAuthenticationController : ApiController
{
public object Post(string[] stuff)
{
try
{
return GeneralFunctions.CheckAuthentication(stuff[0].ToString(), stuff[0].ToString());
}
catch
{
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NoContent));
}
}
}
这就是我的帖子在fiddler中的样子:
POST https://localhost:8081/CheckAuthentication HTTP/1.1
User-Agent: Fiddler
Content-Type: application/json
Host: localhost:8081
Content-Length: 43
{"userName":"un","userPassword":"password"}
但是当我调试时,“stuff”总是为空。
有人可以帮忙吗?
答案 0 :(得分:2)
这是一个javascript对象(json)而不是字符串数组。
javascript中的字符串数组如下所示:
["this","is","a","string","array"]
并且对象看起来像:
{this:"is", a:"string", object:"ok"}
您需要添加如下类:
class LoginData
{
public string Username {get;set;}
public string UserPassword {get;set;}
}
public class CheckAuthenticationController : ApiController
{
public object Post(LoginData loginData)
{
try
{
return GeneralFunctions.CheckAuthentication(loginData.Username , loginData.UserPassword );
}
catch
{
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NoContent));
}
}
}
这应该是你的json帖子:
{Username : 'user', UserPassword: 'pass'}