Webapi数据没有受到限制 [英] Webapi data is not getting bound

查看:136
本文介绍了Webapi数据没有受到限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由Visual Studio 2015向导生成的WebAPI项目。在其中,我添加了一个包含GET处理程序的控制器:

I have a WebAPI project which was generated by the Visual Studio 2015 wizard. In it, I have added a controller that contains a GET handler:

public Task<string> Get (int id)
{
    using (var wb = new WebClient())
    {
        var data = new NameValueCollection();
        data["username"] = "myUser";
        data["password"] = "myPassword";

        var response = wb.UploadValues(<project URL, ends with a value for the id  
            parm>, "POST", data);
    }

    return null;
}

And I have the following method:

[HttpPost]
public NameValueCollection Post(int id, [FromBody] NameValueCollection data)
{
    return data;
}



我发现虽然我故意硬编码GET处理程序中的数据,但是当调用POST方法时,data参数总是有一个.Count为0.返回GET后,它的.Count为2,与开始时完全相同。我已经尝试了十几种不同的调用POST的方法,这些方法取自各种在线文章。在每种情况下,行为都是相同的:POST方法不接收任何数据。 id参数始终具有传递给GET方法的正确值。



有没有人知道可能出现的问题?



我尝试了什么:



我在网上发现的文章中尝试了十几种不同的方法。


I find that although I have deliberately hard-coded the data in the GET handler, when the POST method gets invoked, the data parameter always has a .Count of 0. Upon return to the GET, it has a .Count of 2, exactly as it started out. I have tried a dozen different methods of invoking the POST, taken from various articles online. In every case, the behavior is the same: the POST method receives no data. The id parameter always has the correct value passed in to the GET method.

Does anyone have a clue about what might be going wrong?

What I have tried:

I have tried a dozen different methods from articles found on the web.

推荐答案

首先,你真的需要在尝试之前学习基本的C#。为什么?



因为你的Get方法是硬编码的,根本不返回任何内容。请参阅方法底部的 return null 。是的。



接下来,你的Get方法说它返回任务< string>< / string> ,但它没有做任何类似于设置和返回返回字符串的任务的事情。



我希望我能告诉你如何修复这个,但我有不知道你想做什么。你有一个Get方法,不是获取和返回任何东西。它实际上是将值放入其他网址,无论是什么。你的代码充其量是精神分裂症。
First, your REALLY need to learn basic C# before you try this. Why?

Because your Get method is hard coded to return nothing at all. See that return null at the bottom of the method. Yeah.

Next, your Get method says it's returning a Task<string></string>, but it's not doing anything that resembles setting up and returning a Task that returns a string.

I wish I could tell you how to "fix" this, but I have no idea what you're trying to do. You've got a Get method that isn't Getting and returning anything. It's actually Putting values into some other web URL, whatever that is. Your code is schizophrenic at best.


这篇关于Webapi数据没有受到限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆