当前位置 > 首页 > Asp.net

Binding form data in ASP.NET Web API

8/10/2012 11:23:00 PM来源:Asp.net

One scenario that is very common in ASP.NET MVC is to bind form data (data posted with the media type application/x-www-form-urlencoded)  to individual parameters or a form collection in a controller action. However, that scenario does not work quite the same in ASP.NET Web API as the body content is treated a forward-only stream that can only be read once. 

If you define an action with multiple simple type arguments such as the one shown bellow, the model binding runtime will try to map those by default from the request URI (query string or route data). 

public void Post(int id, string value)
{
}

That data never comes from the body content unless you decorate the argument with the [FromBody] attribute. However, only one argument in the action can be decorated with this attribute, and you get an exception when you try to decorate more than one. Nevertheless, the desired effect is far from what you would expect.

If you define an action as follow,

public void Post([FromBody]int id)
{
}

And you POST a form with the following data,

http://localhost:64561/api/values

METHOD: POST

CONTENT-TYPE: application/x-www-form-urlencoded

BODY: id=4

Nothing will be mapped. It does not matter you defined a single parameter with the same as the one posted in the body.

If you really want to have a single type argument mapped to the body content, you have to POST a message with the following content

http://localhost:64561/api/values

METHOD: POST

CONTENT-TYPE: application/x-www-form-urlencoded

BODY: =4

As you can see, no key defined so the whole is mapped to our argument in the action. That’s the default behavior for single types. If you want to change that behavior, the framework is completely extensible and you can replace the whole model binding infrastructure included out of the box for one that you find more useful as it is explained here by Mike Stalls (which shows how you can replicate the same model used by ASP.NET MVC).

For complex types, the framework includes two MediaTypeFormatter implementations. The first one is FormUrlEncodedMediaTypeFormatter, which knows how to map every key/value in the form data to a FormDataCollection or a JTokenType (a dynamic type for expressing JSON available as part of the Json.NET library). These two are useful when you want to get access to all the values in form in a generic manner. You define for example a controller that receives a FormDataCollection instance like this,

public void Post(FormDataCollection form)
{
}

The second one is JQueryMvcFormUrlEncodedFormatter, which derives from the first one and knows how to map the form data to a concrete type representing a model. For example, a model defined as follow will match a body content with the keys “Id” and “Value”

public class MyModel
{
   public int Id { get; set; }
   public string Value { get; set; }
}

Therefore, your controller action can either receive a generic form collection (or a JTokenType) or a complex model type as any of these two will handle the deserialization of the body content type into the expected model.



  • NPOI 1.2教程 - 2.2.1 设置单元格格式

    3/27/2009 10:12:00 AM

    摘要: 作者:Tony QuNPOI官方网站:http://npoi.codeplex.com/在Excel中我们经常要设置格式,比如说日期格式(yyyymmdd)、小数点格式(1.20)、货币格式

  • 李开复新公司已经成立

    9/8/2009 12:56:00 PM

    摘要: 李开复新公司已经成立--创新工场,现在已经开始招人了,有能力不防试试,招聘职位请点链接:http://www.innovation-works.com/Recruitment.asp创新工场

  • Resizing images from the server using WPF/WIC instead of GDI+

    12/12/2009 2:53:00 AM

    Bertrand Le Roy has this on his blog today: http://weblogs.asp.net/bleroy/archive/2009/12/10/resiz

  • 真正的三层结构,初学者的最佳入门。

    12/22/2009 3:50:00 AM

    阅读: 117 评论: 5 作者: ATGO 发表于 2009-12-22 00:19 原文链接 我很爱国! 今天给大家解释一下我对三层结构的理解,也希望能为初

  • VTemplate模版引擎的使用--进阶篇

    9/3/2009 5:15:00 PM

    摘要: VTemplate是一个免费的开源(采用LGPL开源许可协议)模板引擎,用于解析运行VT模板; 其主要目标是为ASP.Net开发提供另外一种技术选择方案,以保证用简单的语法,良好的结构,不混

  • 挣脱浏览器的束缚(2) - 别让脚本引入坏了事

    1/22/2007 11:48:00 AM

    摘要: 现在哪里还找得到不引入JavaScript脚本文件的Web应用?使用脚本文件的好处多多,其中最重要的可能就是提供缓存能力了。使用脚本文件之后再加上缓存,可以大大降低数据传输量,提高页面打开的

  • C#对游戏手柄的编程开发-API篇(1)

    3/26/2009 1:37:00 AM

    摘要: Flash中不支持对游戏手柄进行编程,但我们可以换种方法,做一个辅助程序(外挂? ),将手柄中的操作事件转换为Flash中可接受的键盘与鼠标操作事件,这样不就可以使用游戏手柄来玩Flash游

  • 发布SoanCode代码生成器 V3.0.0.0 ,自动生成的项目多达10个

    8/30/2007 5:46:00 PM

    经过一个多星期的努力,终于把 V3.0弄出来了,为了加这个聚合缓存依赖,几乎把以前的代码都重构了一遍,变得有条理多了, 点击这里下载 这是直接查看代码的效果,如果缓存结构选了聚合缓存依

  • Net程序集运行报异常:ECall 方法必须打包到系统模块中

    10/23/2012 10:26:00 AM

    有关调用实时(JIT)调试而不是此对话框的详细信息, 请参见此消息的结尾。 ************** 异常文本 ************** System.Security.Securi

  • Automated web testing (1) Using WatiN

    2/5/2009 1:12:00 AM

    摘要: WatiN, stands for Web Application Testing In .NET, pronounced as what-in, is an easy to use, fe