有没有办法在Web API控制器来处理表单POST数据? [英] Is there a way to handle form post data in a Web Api controller?

查看:163
本文介绍了有没有办法在Web API控制器来处理表单POST数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.NET MVC ,可以访问表单POST数据:

In ASP.NET MVC , one can access the form post data:

var thisData = Request.Form["this.data"];

是否有可能实现在的Web API相同的功能 ApiController?

推荐答案

的ASP.NET Web API已成为在处理不同的HTTP方案显著更可靠的 - 尤其是流媒体。作为这种只媒体类型格式化通常触摸的内容和具有使内容的连贯性。

ASP.NET Web API has become significantly more robust in dealing with different HTTP scenarios - especially streaming. As such only media type formatters normally touch the content and have to make a coherence of the content.

在ASP.NET MVC中,应用程序/ x-WWW的形式urlen codeD 内容类型是一等公民(尤其是对待,因为这是的POST请求的95%的内容类型),我们有 FormsCollection 来提供接入字典访问,当它被定义为输入参数。

In ASP.NET MVC, application/x-www-form-urlencoded content type is a first class citizen (and treated especially since this is the content type of 95% of the POST requests) and we have the FormsCollection to provide dictionary access in access, whenever it is defined as an input parameter.

在ASP.NET的Web API,应用程序/ x-WWW的形式urlen codeD 是另一个内容类型,并应该是其MediaTypeFormatter阅读。这样的ASP.NET Web API无法做出关于形式的任何假设

In ASP.NET Web API, application/x-www-form-urlencoded is yet another content type, and supposed to be read by its MediaTypeFormatter. As such ASP.NET Web API cannot make any assumption about the Forms.

在的ASP.NET Web API通常的做法是重新present的形式的模型,因此媒体类型格式反序列化。另一种方法是定义动作的参数为的NameValueCollection

The normal approach in ASP.NET Web API is to represent the form as a model so the media type formatter deserializes it. Alternative is to define the actions's parameter as NameValueCollection:

public void Post(NameValueCollection formData)
{
   var value = formData["key"];
}

这篇关于有没有办法在Web API控制器来处理表单POST数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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