Tracing ASP.NET MVC 3 RTM Source Code
1 |
Trying to understand how the magic model binding works in ASP.NET MVC, so I managed to include ASP.NET MVC 3 source projects to my solution, here are my steps to make ASP.NET MVC 3 RTM source code traceable:
為了搞懂ASP.NET MVC中神奇的Model Binding原理,花了點功夫讓我的專案可以納入ASP.NET MVC 3原始碼一起追蹤,順手整理成心得以饗同好。
- Download ASP.NET MVC 3 RTM Source Code from CodePlex and unzip it.
- Create a new ASP.NET MVC 3 Web Application project in Visual Studio 2010 ( choose Razor view engine in my case )
- Add System.Web.Mvc, System.Web.Razor, System.Web.WebPages, System.Web.WebPages.Deployment, System.Web.WebPages.Razor, totally 5 projects, to the solution. ( projects are under mvc3-rtm-sources/mvc3/src and mvc3-rtm-sources/webpages/src )
- Remove original reference to System.Web in test project ( TraceMVCSource in my case )
- Add reference to the project System.Web.Mvc.
- Run the test project, a compilation error is returned because of assembly versioning issue.
c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\ebd5a45d\6aa2a371\App_Web_index.cshtml.a8d08dba.lfcln4mj.0.cs(29): error CS0433: The type 'System.Web.Mvc.WebViewPage<TModel>' exists in both 'c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\ebd5a45d\6aa2a371\assembly\dl3\6dc30b64\304041e0_44f6cb01\System.Web.Mvc.DLL' and 'c:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Web.Mvc\v4.0_3.0.0.0__31bf3856ad364e35\System.Web.Mvc.dll' - Modify /web.config, remove the strong name part of System.Web.Mvc and System.Web.WebPages.
<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<!--
<add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
-->
<add assembly="System.Web.Mvc" />
<add assembly="System.Web.WebPages" />
</assemblies>
</compilation>
- Run the test project again, another exception gives us the hint about something to do with Razor view engine configuration.
[A]System.Web.WebPages.Razor.Configuration.HostSection cannot be cast to [B]System.Web.WebPages.Razor.Configuration.HostSection. Type A originates from 'System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' in the context 'Default' at location 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages.Razor\v4.0_1.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll'. Type B originates from 'System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'Default' at location 'C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\ebd5a45d\6aa2a371\assembly\dl3\c825e379\b33a46df_44f6cb01\System.Web.WebPages.Razor.DLL'.
- Modify /Views/web.config, remove Version, Culture and PublicKeyToken parts from these settings. (with yellow background color)
<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor"
type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor"><section name="host"
type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor"
requirePermission="false" /><section name="pages"
type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor"
requirePermission="false" /></sectionGroup>
</configSections>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
</namespaces>
</pages>
</system.web.webPages.razor>
Now we can trace into MVC 3 source code in debugging mode, have fun!
Comments
# by KKBruce
使用MVC Source來追蹤,對於學習MVC真的很重要。感謝分享。