【问题标题】:How to retrieve HttpPost parameters in c#如何在 C# 中检索 HttpPost 参数
【发布时间】:2015-06-19 15:23:30
【问题描述】:

所以我知道这是可行的:

[HttpPost]
public string functionthatiuse()
{
    string id = "";//does nothing
    return relevantinfo;
}

然后我使用下面显示的这个 Chrome POST 扩展,我在函数中有一个断点,我知道它是如何到达它的。这基本上是一个空的帖子请求。

但是当我尝试使用参数发布时,我遇到了麻烦。理想情况下,我想做这样的事情:

[HttpPost]
public string functionthatiuse(string idx)
{
    string id = ""; //does nothing and is different from idx
    return relevantData;
}

但是当我尝试使用它时,我得到了一个错误。我很确定这是因为我没有正确格式化内容正文,并且我尝试将其他内容放入内容正文中,但没有任何效果。有谁知道我如何使用这个扩展向这个函数发送 POST 参数?我在代码中所做的格式应该基本相同(要求的一部分)。

编辑:

这是错误的图片:

【问题讨论】:

  • 什么错误?您在失败的请求中发送了什么?
  • 什么时候出现错误?之前是string id = "";
  • 我实际上不知道我应该发送什么。所以我已经尝试了你在上面看到的。我试过 [idx=hello]、[idx="hello"]、[randomtext]。所以这基本上就是问题所在。我不确定
  • 代码没有错误。它在http post请求中。它甚至没有到达代码。
  • 你能把应该调用这个 Post 方法的表单的代码放上去吗?

标签: c# http http-post http-post-vars


【解决方案1】:

根据这里的微软:http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api

您必须在参数列表中添加“[FromBody]”。您只能拥有这些类型的参数之一。

同样在 chrome post extension 下的 headers 中你需要输入:

名称:内容类型 值:application/json

【讨论】:

    猜你喜欢
    • 2011-12-28
    • 1970-01-01
    • 2017-04-01
    • 2016-11-01
    • 1970-01-01
    • 2011-07-02
    • 2021-12-11
    • 2011-02-04
    • 1970-01-01
    相关资源
    最近更新 更多