POST 不接受 int 参数

发布于 2024-12-07 01:56:48 字数 1249 浏览 0 评论 0原文

我在使用 ASP.NET MVC3 时遇到问题。

我正在尝试将一些参数发布到 JSONResult 操作。当我使用 GET 时它效果很好,但是当我使用 POST 时,我收到一个错误,因为显然在使用 POST 时,它不会接受我的整数参数,并说参数字典包含它们的空值。

有人以前遇到过这个吗?谢谢。

编辑:根据要求编写一些代码:

public JSONResult PostingHere(string username, int someotherparameter) {
    //Code   
}

真的,这才是最重要的,因为在 POST 时会抛出异常,因为显然有一个空值传递给了 someotherparameter 参数。当使用 GET 时,如前所述,它的工作效果非常好。

预期用途是从 iPhone 发送 NSURLRequest,但我一直在使用名为 Poster 的 Firefox 扩展进行调试,得到了相同的结果。完整错误:

参数字典包含方法“System.Web.Mvc.JsonResult PostingHere(System.String, Int16)”的不可空类型“System.Int16”的参数“someotherparameter”的空条目

编辑2:根据请求,我将添加一个调用导致错误的操作的示例:

<form action="http://fullurlhere.com/Controller/PostingHere" method="POST">
<input type="hidden" name="userid" value="astring" />
<input type="hidden" name="someotherparameter" value="100" />
<input type="submit" value="Brace for impact" />
</form>

编辑3:发布数据;不要介意变量名称的差异,这些是它们的实际名称。

Post的结果

编辑4:更神秘的是,当在我自己的计算机上对本地服务器进行测试时,问题消失了,服务器正在运行相同的 MVC 项目。所以问题很可能出在服务器端设置中。

已解决:结果当我评估请求时,服务器将我从 http:// 重定向到 https://,导致我丢失了发布的数据。感谢大家提供如此出色的运动。

I have a problem with ASP.NET MVC3.

I'm trying to post some parameters to a JSONResult action. It works great when i use GET, but when i use POST, i get an error, because apparently when using POST, it won't accept my integer parameters as such, and say that the parameters dictionary contains a null value for them.

Anyone run across this before? Thanks.

Edit: some code, as requested:

public JSONResult PostingHere(string username, int someotherparameter) {
    //Code   
}

Really, that's all that matters, because the exception is thrown then and there when POSTing because apparantly a null value gets passed to the someotherparameter parameter. When using GET, as said before, it works just dandy.

The intended use is to post with a NSURLRequest from an iPhone, but I've been debugging with a firefox extension called Poster and i get the same result. Full error:

The parameters dictionary contains a null entry for parameter 'someotherparameter' of non-nullable type 'System.Int16' for method 'System.Web.Mvc.JsonResult PostingHere(System.String, Int16)'

Edit 2: By request, I'll add an example of calling the action which results in the error:

<form action="http://fullurlhere.com/Controller/PostingHere" method="POST">
<input type="hidden" name="userid" value="astring" />
<input type="hidden" name="someotherparameter" value="100" />
<input type="submit" value="Brace for impact" />
</form>

Edit 3: The post data; never mind the discrepancy in variable names, these are the actual names of them.

Result of the Post

Edit 4: Adding to the mystery, the problem disappears when testing against a local server on my own computer, with the very same MVC Project the server is running. So the problem is likely within a server-side setting.

Solved: Turns out when i evalutated the request, that the server was redirecting me to https:// from the http://, causing me to lose my POSTed data. Thanks to everyone for being such good sports.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

全部不再 2024-12-14 01:56:48

尝试

public JSONResult PostingHere(string username, int? someotherparameter) {
    //Code   
}

try

public JSONResult PostingHere(string username, int? someotherparameter) {
    //Code   
}
醉梦枕江山 2024-12-14 01:56:48

已解决:结果当我评估请求时,服务器将我从 http:// 重定向到 https://,导致我丢失了发布的数据。感谢大家提供如此出色的运动。

Solved: Turns out when i evalutated the request, that the server was redirecting me to https:// from the http://, causing me to lose my POSTed data. Thanks to everyone for being such good sports.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文