PS: For more devtextArticlePlease refer to the following link for more information:
Http://www.n-pei.com/blog/devtext_widget_Create
Http://www.n-pei.com/blog/devtextdesign3
Http://www.n-pei.com/blog/tools-introduce
And so on ......
Yesterday, many people complained that they didn't use autofac. So today, after deploying Cr, I will explain how to integrate the IOC tool autofac in Asp.net MVC. It also describes the fluentvalidation used in devtext ....
1. Create an Asp.net MVC applicationProgram:
2. Add autofac reference
If you have installed nuget, you do not need to download the generated DLL package from the official autofac website. Instead, you can use vs2010 to obtain the package automatically.
This section describes how to use nuget to automatically add autofac references.
Again, the list-package is used to list all packages that have been packaged by nuget. You can see autofac.
Next, use Install-package to automatically add references.
Because we use nuget to automatically add DLL, We need to manually configure web. config. Add the following configuration to httpmodule:
3. Configure the Gobal. asax File
First, you need to let this class inherit the icontainerprovideraccessor interface and declare an icontainerprover as the application container.
The second step is to register the controller and some models and modelbinder in the application_start method, and use the custom controllerfactory in autofac.
The above completes Asp.net MVC integration of autofac.
Next, we will use autofac and fluentvalidation to explain how fluentvalidation works.
We use iperson and class person to describe.
Register person in global. asax.
Validation section:
Add reference first:
Validation'sCodeStyle:
The last part is the Controller part and the view part:
Note in the Controller that we use the controllerfactory of autofac, so the construcotr of the controller can have n parameters by default, instead of not allowing parameters.
View code:
Finally, do not forget to register our validation provider in global. asax.
Run the following command by pressing F5: