C# 如何使用WebClient类将值从一台服务器发布到另一台服务器?

C# 如何使用WebClient类将值从一台服务器发布到另一台服务器?,c#,asp.net-web-api,C#,Asp.net Web Api,我正在尝试使用WebAPI技术将请求从一台服务器发送到另一台服务器。 这是我接收调用的方法 [HttpGet] [HttpPost] public HttpResponseMessage MyMethod([FromBody] string token, [FromBody] string email, [FromBody] string password) { string a = "hello world!"; return new

我正在尝试使用WebAPI技术将请求从一台服务器发送到另一台服务器。 这是我接收调用的方法

[HttpGet]
    [HttpPost]
    public HttpResponseMessage MyMethod([FromBody] string token, [FromBody] string email, [FromBody] string password)
    {

        string a = "hello world!";
        return new HttpResponseMessage() { Content = new StringContent(a) };
    }
我正在使用此代码发布到:

using (var c = new WebClient())
        {
            //string obj = ":" + JsonConvert.SerializeObject(new { token= "token", email="email", password="password" });
            NameValueCollection myNameValueCollection = new NameValueCollection();

            // Add necessary parameter/value pairs to the name/value container.
            myNameValueCollection.Add("token", "token");
            myNameValueCollection.Add("email", "email");

            myNameValueCollection.Add("password", "password");

            byte[] responseArray = c.UploadValues("MyServer/MyMethod", "POST", myNameValueCollection);


            return Encoding.ASCII.GetString(responseArray);
        }
我试过几种选择

我上面写的这篇文章给了我一个内部服务器错误,我的方法中的断点没有命中,所以问题不在我的方法代码上

注释向我的nameValueCollection添加参数的三行,我得到一个404

从MyMethod的签名中删除参数,它就工作了

我想将此信息发布到承载API的服务器上


你知道我做错了什么吗?

通常从编写视图模型开始:

public class MyViewModel 
{
    public string Token { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
}
控制器操作将作为参数:

[HttpPost]
public HttpResponseMessage MyMethod(MyViewModel model)
{

    string a = "hello world!";
    return new HttpResponseMessage() { Content = new StringContent(a) };
}
注意,我已经去掉了
[HttpGet]
属性。你得选择动词。顺便说一下,如果您遵循标准的ASP.NET Web API路由约定,则操作的名称应与用于访问它的HTTP谓词相对应。这是一个标准的安静的习惯

现在您可以点击它:

using (var c = new WebClient())
{
    var myNameValueCollection = new NameValueCollection();

        // Add necessary parameter/value pairs to the name/value container.
    myNameValueCollection.Add("token", "token");
    myNameValueCollection.Add("email", "email");
    myNameValueCollection.Add("password", "password");

    byte[] responseArray = c.UploadValues("MyServer/MyMethod", "POST", myNameValueCollection);

    return Encoding.ASCII.GetString(responseArray);
}