ASP. NET mvc3 series tutorial-how to make project debug into mvc3 source code

Source: Internet
Author: User
ArticleDirectory
    • I: How to debug mvc3 source code.
I: How to debug mvc3 Source code .

1. About the source of ASP. NET mvc3CodeObtain Method

ASP. net MVC since 1.0 to 3.0, its source code has been fully open to the outside world, and it has also attracted many developers who used to use webform to study MVC for this reason, I am also very happy to hear from all of you in the IT industry .. the emergence of today's mvc3 version .. I don't know if the next version will be .......
The source code of the original question is hosted on Microsoft's codeplex. The connection below is the project address of ASP. net mvc, which can be easily obtained:

Http://aspnet.codeplex.com/wikipage? Title = MVC & referringtitle = home

After downloading it, let's take a look at the distribution of various MVC components in the source code package:

Mvc3 source code package file name: mvc3-rtm-sources.zip

The following figure describes the main components of mvc3.ProgramLocation of the Set in the source code package

2. The following describes how to create a visual Studio MVC project that can debug the mvc3 source code.

Create a blank solution dotnetdr_mvc3_exampleproject from vs2010, disable the solution in Vs, and copy the solution file
Mvc3-rtm-sources folder (that is, the location where you extract the mvc3 source code file, here my path is relatively long. My location is:
D: \ svn_workfolder \ dotnetdr \ project \ code \ mvc3-rtm-sources, please perform related operations according to your actual situation ).

Then add the following MVC project files to the blank solution we have created and execute the compilation. The final solution Resource Manager diagram is as follows:

During the compilation process on this computer, it went smoothly! No error, no warning !~ _~....

Create a solution folder named webapp and add a new mvc3 project named mvc3_app1. then, remove the mvc3 assembly that was originally referenced from the GAC, and add the mvc3 projects that reference this solution:
 
The final mvc3_app1 scenario is:
 

Then, the web project compilation is completed, but if you run the program, the local assembly conflicts with the GAC assembly.

[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 \ 7c2060f6 \ 6145b34e \ Assembly \ dl3 \ 9036b7c7 \ fc8023c0_73fecb01 \ system. web. webpages. razor. dll '.

Previously, I had the idea of uninstalling the mvc3 assembly that fell into the GAC. However, due to the association between the Assembly and Microsoft Windows Installer, gacutil/UF cannot be used to unload the assembly.

After several hard work, I finally understood the specific cause of the conflict:

Fortunately, the previous practice failed. The real reason is that in Web pages 1.0, he is still on the web. A configuration node is added in config. Currently, it is certain that developers can cancel the default mvc3 configuration environment as needed. of course, this requires developers to have a deep understanding of mvc3.

Web pages 1.0 in Web. config, I have found a new configuration node:
Path in ~ /Views/Web. config. Instead of Web. config. Can I use web. config? Still to be studied.

 
<Configuration> <System. Web. webpages. Razor/> <! --Note that the same level of system. Web--> <System. Web/>...</Configuration>

I have not found a detailed description of this configuration node on msdn. you can only find the list of related class library members. the detailed description is not complete yet. we have to wait for Ms documentation staff to complete. ~

Then we can open it and modify the relevant publickeytoken to null. set to allow. net runtime references the unsigned mvc3 source code project in our solution instead of being referenced in GAC. in this way, we can eliminate the possibility of conflicts between different sets of programs with the same name type. ~ This feature also improves class library conflicts of different versions of the same name compared with the previous XX (such as COM +, which I don't know much about in that age ).

Next we will modify the ~ in our mvc3_app1 project ~ /Views/Web. config file

 <  XML     Version  = " 1.0 " > <  Configuration  > <  Configsections  > <  Sectiongroup    Name  = " System. Web. webpages. Razor "    Type  = " System. Web. webpages. Razor. configuration. razorwebsectiongroup, system. Web. webpages. Razor, version = 1.0.0.0, culture = neutral, publickeytoken = NULL " > <! --  This line needs to be changed  --> <  Section     Name = " Host "    Type  = " System. Web. webpages. Razor. configuration. hostsection, system. Web. webpages. Razor, version = 1.0.0.0, culture = neutral, publickeytoken = NULL "    Requirepermission  = " False " /> <! --  This line needs to be changed  --> <  Section    Name  = " Pages "    Type  = " System. Web. webpages. Razor. configuration. razorpagessection, system. Web. webpages. Razor, version = 1.0.0.0, culture = neutral, publickeytoken = NULL "    Requirepermission  = " False " /> <! --  This line needs to be changed --> </  Sectiongroup  > </  Configsections  > <  System. Web. webpages. Razor  > <  Host     Factorytype  = " System. Web. MVC. mvcwebrazorhostfactory, system. Web. MVC, version = 3.0.0.0, culture = neutral, publickeytoken = NULL " /> <! --  This line needs to be changed --> <  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  > ...

Then modify ~ /Web. config file

 <  Configuration  > <  Connectionstrings  /> <  Appsettings  > <  Add    Key  = " Webpages: Version "    Value  = " 1.0.0.0 " /> <  Add     Key  = " Clientvalidationenabled "    Value  = " True " /> <  Add     Key  = " Unobtrusivejavascriptenabled "    Value  = " True " /> </  Appsettings  > <  System. Web  > < Compilation     Debug  = " True "    Targetframework  = " 4.0 " > <  Assemblies  > <  Add     Assembly  = "System. Web. Export actions, 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 " /> <! --  This line needs to be changed  --> <  Add     Assembly  = " System. Web. MVC, version = 3.0.0.0, culture = neutral, publickeytoken = NULL " /> <! --  This line needs to be changed  --> <  Add    Assembly  = " System. Web. webpages, version = 1.0.0.0, culture = neutral, publickeytoken = NULL " /> </  Assemblies  > </  Compilation  > ... </  System. Web  > ... </  Configuration  > 

Here I found a hard-coded publickeytoken:
Preapplicationstartcode. CS file line 24 in project system. Web. webpages. Deployment:

I have a deep understanding of the functions of this constant.

After the modification is complete, we set the relevant breakpoint.

Place breakpoint 1 at system. Web. webpages. Deployment preapplicationstartcode. CS line: 39 in the root directory of the project

Place breakpoint 2 at the homecontroller. CS line: 12 in the controllers folder of the mvc3_app1 Project
Press F5 to debug the mvc3_app1 project, and then debug the project smoothly.

For the process of APP initialization, I have previously written articles about how webpages are run out of webforms. If you are interested, review the article.

We can see that the running point has reached the action, which means we are very close to success.

Run the program directly and you can see that this blue page has proved that all the MVC source programs we have referenced can work normally. Are you as excited as the bloggers at this time?

Even after this round, this chapter was successfully completed and opened a window to learn about the internal working principles of mvc3.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.