Asp.net MVC integrates autofac and fluentvalidation

Source: Internet
Author: User

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:

 

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.