MVC 3 working with Area

Source: Internet
Author: User
Tags actionlink

Area is similar to the namespace concept in route. It can separate multiple sets composed of controller, view, and route, which is useful in relatively large projects.

1. Create an area

Create an MVC project and right-click "add area" on the solution ".

After adding the project, you will find that there is a top-level directory area in the solution. Below is a small MVC project.

2. register an area

In the application_start () method, arearegistration. registerallareas ();

3. Enter area/controller/action in the address bar to access the Controller and other content in area.

4. By default, you need to add a namespace in route registration to prevent mixed controller names.

View code

 1   Public   Static   Void  Registerroutes (routecollection routes ){  2 Routes. ignoreroute ("  {Resource}. axd/{* pathinfo}  "  );  3   Routes. maproute (  4   "  Default  " , //  Route name  5   "  {Controller}/{action}/{ID}  " ,//  URL with Parameters  6   New {Controller = "  Home  " , Action = "  Index  " , Id = Urlparameter. optional}  7   );  8 }
View code

1   Public   Static   Void  Registerroutes (routecollection routes ){  2 Routes. ignoreroute ( "  {Resource}. axd/{* pathinfo}  "  );  3   Routes. maproute (  4   "  Default  " ,//  Route name  5   "  {Controller}/{action}/{ID}  " , //  URL with Parameters  6   New {Controller = "  Home  " , Action = "  Index  " , Id =Urlparameter. optional },  7   New [] { "  Workingwithareas. Controllers  "  }  8   );  9 }

5. By default, you do not need to change anything. The @ html. actionlink () method will automatically detect your current area and connect to and navigate to the area. But if you need to connect to another area, you need to write the following method:

View code

1 3812@ Html. actionlink ("Click me to go to another area","Index",New{Area ="Support"})

Note: The value of area must be a valid value.

 

 

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.