Areas in the "MVC5" (areas)

Source: Internet
Author: User

MVC itself advocates separation of concerns. But when the business logic of the project itself is complex enough, if all the business logic is written under the Controller folder, you will see a very large variety of named controllers, this time the role of the region is very obvious, the different modules through the region to distinguish, simple and clear.

To add a zone:
The new MVC project is no region. Click on the Add zone, right side of the project, enter a region name.. NET automatically adds the "Areas" folder under the project and creates a folder for the corresponding zone under that folder. Create a "test" area, add the controller "default" under the zone, and change the "default" controller content to:

[Routearea ("test")]    [Routeprefix ("default")]      Public class Default1controller:controller    {        [Route ("index")]        public  ActionResult Index ()        {            return  View ();        }    }

To register a route:

The MVC project creates regrets for automatically generating file "global.asax" files. Locate the Routeconfig.registerroutes (routetable.routes) in the Application_Start function. Enter RegisterRoutes, where you add code Routes. Mapmvcattributeroutes ()

 Public classRouteconfig { Public Static voidregisterroutes (routecollection routes) {routes. Ignoreroute ("{Resource}.axd/{*pathinfo}"); Routes. Mapmvcattributeroutes ();//Registering attribute Routesroutes. MapRoute (Name:"Default", URL:"{Controller}/{action}/{id}", defaults:New{controller ="Home", action ="Index", id =urlparameter.optional}); }    }

Delete arearegistration:

When you create a new area, the project automatically creates arearegistration under the zone. For example, when the default controller is created, "TestAreaRegistration.cs" is automatically generated under the folder in the test area, which is used to register the route in the previous version of MVC5. In the MVC5 because the attribute route is registered in the Global.asax, the file can be deleted directly, otherwise there will be no routing errors found. The content is roughly:

  Public classtestarearegistration:arearegistration { Public Override stringAreaName {Get            {                return "Test"; }        }         Public Override voidRegisterarea (AreaRegistrationContext context) {context. MapRoute ("Test_default",                "Test/{controller}/{action}/{id}",                New{action ="Index", id =urlparameter.optional}); }    }

The controller in the areas can then be accessed directly through a routed approach:

<class= "btn btn-default"  href= "/test1/default/index"  >&raquo; </ a >

Areas in the "MVC5" (areas)

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.