当前位置 > 首页 > Asp.net

Fixing the Model Binding issue of ASP.NET MVC 4 and ASP.NET Web API

2012-12-8 21:01:00来源:Asp.net

        Introduction:


                    Yesterday when I was checking ASP.NET forums, I found an important issue/bug in ASP.NET MVC 4 and ASP.NET Web API. The issue is present in System.Web.PrefixContainer class which is used by both ASP.NET MVC and ASP.NET Web API assembly. The details of this issue is available in this thread. This bug can be a breaking change for you if you upgraded your application to ASP.NET MVC 4 and your application model properties using the convention available in the above thread. So, I have created a package which will fix this issue both in ASP.NET MVC and ASP.NET Web API. In this article, I will show you how to use this package.  


        Description:


                    Create or open an ASP.NET MVC 4 project and install ImranB.ModelBindingFix NuGet package. Then, add this using statement on your global.asax.cs file,


         using ImranB.ModelBindingFix;

                    Then, just add this line in Application_Start method,  


	Fixer.FixModelBindingIssue();
	// For fixing only in MVC call this
	//Fixer.FixMvcModelBindingIssue();
	// For fixing only in Web API call this
	//Fixer.FixWebApiModelBindingIssue();

.



                    This line will fix the model binding issue. If you are using Html.Action or Html.RenderAction then you should use Html.FixedAction or Html.FixedRenderAction instead to avoid this bug(make sure to reference ImranB.ModelBindingFix.SystemWebMvc namespace). If you are using FormDataCollection.ReadAs extension method then you should use FormDataCollection.FixedReadAs instead to avoid this bug(make sure to reference ImranB.ModelBindingFix.SystemWebHttp namespace). The source code of this package is available at github



        Summary:


                    There is a small but important issue/bug in ASP.NET MVC 4. In this article, I showed you how to fix this issue/bug by using a package. Hopefully you will enjoy this article too.





  • 在单元测试时指定HttpContext的各种Path

    2009-8-21 13:18:00

    摘要: 设置HttpContext中各种Path一直是个问题,因为被测试的方法可能用到各种Path中的任何一个,而各种Path之间有一定关联,如果我们完全手动设置Mock对象的话会是一个浩大的工程。

  • .NET Micro Framework 4.2 bate 源码探析

    2011-6-23 18:52:00

    .NET Micro Framework 4.2 bate发布已经有一段时间了,一直没有腾出时间研究,昨天因为LWIP协议栈的原因(感觉上一个版本有点问题)刚 下了代码,所以抽空研究了一下。

  • c#下实现ping操作

    2009-8-16 23:52:00

    摘要: C#下实现Ping操作。介绍了3种方法,1、传统方式(Socket类实现ICMP协议) 2、调用Ping并捕捉输出 3、利用2.0的Ping类。 --------------------

  • .NET 4中的Windows Workflow Foundation

    2010-4-6 8:09:00

    [原文发表地址] Windows Workflow Foundation in .NET4 [原文发表时间] Monday, March 22, 2010 4:17 PM .NE

  • silverlight Visifire图表转图片--偷天换日的做法

    2009-7-7 19:59:00

    silverlight不支持打印,不支持将元素转图片,所以很麻烦。所以采取一种取巧的做法. 目前我们用VisifireChart来作为图表呈现,这个开源软件同时支持silverlight和wp

  • .NET Framework 4 的库(lib)文件都保存在硬盘的什么位置?

    2011-9-10 12:42:00

    .NET Framework 4 的库(lib)文件都保存在硬盘的什么位置?

  • 网页信息抓取-如何获取延迟加载的网页数据

    2009-5-19 18:44:00

    摘要: 抓取网页信息时,有一个一直困扰我的问题,那就是有些网页的信息是动态生成的,网页载入后,接着执行js,js从服务器端获取数据后填充到网页,现在这种网页实现特别比较多,归功于ajax技术的使用了

  • 使用LINQ to XML来查询XML

    2009-4-21 2:30:00

    摘要: 用于XML的语言集成查询(LINQ to XML)允许使用标准查询操作符就像树形操作符一样来查询XML数据,它能够提供类似XPath的导航在后代、祖先和兄弟的XML元素中导航。它简化

  • .Net Framework 4.0: System.IO.File supports now IEnumerable<string>

    2009-10-26 11:23:00

    .Net Framework 4.0 adds also some new and cool features to file system objects. File class has n

  • Asp.Net Mvc: 浅析TempData机制

    2009-4-6 20:38:00

    摘要: 本文讲述Asp.Net Mvc中TempData的机制与作用。 阅读全文 -------------------------- 新闻: TwitterCEO威廉斯:谷歌出10亿美元我也