【发布时间】:2015-04-24 16:53:54
【问题描述】:
我想要实现的最终目标是从 HTTP POST 请求中获取 MULTIPLE PARAMS,所以如果您知道任何其他方法,请告诉我。
我正在使用
- ASP .NET 自托管 MVC,最新版本。
- 本地主机
- 用于路由的属性映射_
- 一些用于 POST 消息测试的 chrome 扩展,我没有忘记在标题中添加
Content-Type: application/json!
我尝试的第一个代码如下所示:
[RoutePrefix("report")]
public class DefaultController : ApiController
{
[HttpPost, Route("error")]
public HttpResponseMessage Echo(string uid, string buildNumber, string log,
string stack, string context, ...)
{
Console.WriteLine("received message: ");
Console.WriteLine(" uid: " + uid);
Console.WriteLine(" buildNumber: " + buildNumber);
Console.WriteLine(" log: " + log);
Console.WriteLine(" stack: " + stack);
...
return Request.CreateResponse(HttpStatusCode.OK);
}
}
但如果我尝试访问路由到控制器的http://localhost:XXX/report/error,我会收到以下消息:
No HTTP resource was found that matches the request URI
我有点想通了,对于 POST 消息,我必须使用 Model 使用单个参数(这是真的吗??),所以我添加了一个名为 ReportModel 的模型并修改如下:
[RoutePrefix("report")]
public class DefaultController : ApiController
{
[HttpPost, Route("error")]
public HttpResponseMessage Echo(ReportModel report)
{
Console.WriteLine("received message: ");
Console.WriteLine(" uid: " + report.Uid);
Console.WriteLine(" buildNumber: " + report.BuildNumber);
Console.WriteLine(" log: " + report.Log);
Console.WriteLine(" stack: " + report.Stack);
...
return Request.CreateResponse(HttpStatusCode.OK);
}
}
现在我得到了参数report 的null 值。请帮我弄清楚如何在 HTTP POST 中获取多个参数。
[(也许?)已解决]
原始数据如下所示:
POST /report/error HTTP/1.1
Host: localhost:10999
Content-Type: application/json
Accept: application/json
Cache-Control: no-cache
{ "Uid":"asdf", "BuildNumber":"qwer", "Log":"hoo", "Stack":"asdfasdf", ...
我在 Postman Chrome 扩展程序中发送了一个“form-data”标签,但它一次又一次地无法工作。 然后我改为“原始”标签,然后它就可以正常工作了。
我仍然不知道是什么问题。不过,还是非常感谢您的帮助。
【问题讨论】:
-
另一种选择是FormCollection,但这更适用于没有固定模型的情况。
-
实际的 POST 是什么样的?
-
首先要检查的是您在 Fiddler 中的 POST 正文。此外,ReportModel 类必须具有用于反序列化的公共属性。
-
@Padraic 我忘了说我已经做到了!
-
@ParkJongBin 如果你已经解决了,你能不能把你的答案发给别人。
标签: c# asp.net-mvc