使用 HTTP PUT 方法 (ASP.NET MVC) 时未填充 Request.Form
我正在尝试处理 HTTP PUT 请求的正文,但似乎 MVC 引擎(或者可能是支撑它的 ASP.NET 堆栈)不会自动解析 &使用正文数据填充请求的 Form 集合。
在进行 POST 时,这确实可以按预期工作。
请注意,请求的 InputStream 属性确实包含预期的数据,显然我可以使用它构建自己的键/值集合,但是我希望 PUT 的工作方式与 POST 相同。
我在这里错过了什么吗?
动作方法示例:
[AcceptVerbs(HttpVerbs.Put)]
public ActionResult Purchase(int id, FormCollection data)
{
// Do stuff with data, except the collection is empty (as is Request.Form)
}
I'm attempting to process the body of an HTTP PUT request, but it seems that the MVC engine (or perhaps the ASP.NET stack underpinning it) isn't automatically parsing & populating the request's Form collection with the body data.
This does work as expected when doing a POST.
Note that the request's InputStream property does contain the expected data, and obviously I can build my own key/value collection using that, however I would have expected PUT to work the same way as a POST.
Am I missing something here?
Example action method:
[AcceptVerbs(HttpVerbs.Put)]
public ActionResult Purchase(int id, FormCollection data)
{
// Do stuff with data, except the collection is empty (as is Request.Form)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
引用 doc:
因此,我建议您编写一个自定义模型类来保存请求数据并将其作为操作参数传递,而不是使用
Request.Form
。默认模型绑定器将自动填充请求流中传递的键/值的属性:Quote from the doc:
So instead of using
Request.Form
I would recommend you writing a custom model class that will hold the request data and pass it as action parameter. The default model binder will automatically populate the properties from the key/values passed in the request stream:Asp.net 不支持对自定义请求进行开箱即用的 PUT。如果您未使用内置功能来生成 PUT url,请尝试在标头、表单或查询字符串中添加值为
PUT
的X-HTTP-Method-Override
。Asp.net does not support PUT out of the box for custom requests. If you are using not the built in capabilities to generate the PUT url try adding
X-HTTP-Method-Override
with value ofPUT
in headers, form, or query string.