当前位置 > 首页 > Asp.net

Binding form data in ASP.NET Web API

2012-8-10 23:23:00来源: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.



  • .NET深入解析LINQ框架(三:LINQ优雅的前奏)

    2012-12-4 13:24:00

    5】.动态LINQ查询(动态构建Expression<T>表达式树) 什么是动态LINQ查询?LINQ的编写是静态的,因为C#是基于静态类型系统原理设计的,在编写时已经确定类型

  • ZedGraph在Asp.net中的应用

    2007-5-16 18:29:00

    由于项目的需求图表显示数据,今天在网上找了一天,终于找到一个不错的控件----ZedGraph,它支持asp,asp.net,vc. 现在最新的版本是5.0,些版本支持.NET 2.0.5.0版本以

  • ASP.NET2.0的Membership中客户化用户密码的设置

    2007-3-9 14:43:00

    摘要: Asp.net2.0提供了一组写好的用户管理控件,比方说CreateUser,Login等。还是比较好用的,也易于扩展。但是在她的CreateUser里面有个用户密码规则却一直不知道该怎么修

  • 用CSS实现像vista一样很酷的透明效果(附漂亮截图)

    2009-5-21 8:33:00

    精灵空间有不少地方都有类似vista的很酷的透明效果。可以先看这个演示: 精灵 部落 如果你对图片保存格式有些了解的话,你会发现 gif 图片是可以保留部分透明效果的,在网页中直接

  • .Net MVC3 中前台异步获取json数据的实例(2)

    2011-5-19 12:13:00

    本节介绍的内容是在上一篇文章的基础上增加的, http://www.cnblogs.com/alvinyue/archive/2011/05/17/2048783.html

  • .net 里的“框架”和“设计模式”各有哪些?

    2012-6-21 9:34:00

    请资深人员,给我指导下。感觉越学越迷惑,比如三层架构他们说只是设计模式,不是框架,对嘛? 开心了就笑,不开心了就过会儿再笑

  • 博客堂新版本公测中

    2007-2-13 1:23:00

    非常兴奋的通知各位,博客堂新版本正在进行公测中。公测地址为: http://beta.joycode.com 。 目前这个公测版本还非常简陋,并且为了调试方便,我将调试功能设置为了true。所以

  • 博客堂2007年“不谈技术”迎春年会预告

    2007-1-22 11:48:00

    各位博客堂堂主以及堂众: 2007年对于喜欢博客堂的朋友将会是一个非常重要的年度。从今年开始,我们将会定期举办一些定期的聚会,目前拟定于每月一期,交流一下业界技术的最新进展。在第一届年会上,我

  • My move to Graffiti

    2009-4-30 3:50:00

    Technorati Tags: ASP.NET , CMS , Graffiti Many folks have asked me to share the results of the eval

  • A C# Riddle

    2007-3-9 6:30:00

    I asked this one during the last blogger dinner, so here it is for everyone else (and those who cou