Usage of area in asp.net mvc 2.0

Source: Internet
Author: User

Recently, many netizens who have followed and used asp.net mvc are looking for a method to separate the front and back-end structure of the website. The official version of Asp.net mvc 1.0 does not provide us with a good solution. For the official version 1.0, the naming prefix is usually used. For example: AdminProductController, UserProductController, and ProductController. However, such names are lengthy. Once a project is large and complex, it becomes very bloated and difficult to manage. And it is not a perfect solution.

Asp.net mvc 2.0 preview 1 provides a solution: area. It is implemented based on project separation. Therefore, we cannot guarantee that this is the same in the official version in the future. It can only be said that it is a temporary solution.

Below, I will list several specific steps. Demonstrate area usage.

Development Environment: VS2008 SP1, ASP. net mvc 2.0 Preview 1

When adding a project, do not add a test project to keep it clean.

Step 0:

Create a Web Project: Main project.

Step 1:

Create a solution folder named Areas on the Solution

In the Areas solution folder, add a new web project and the Administrator background project.

Step 2:

Clear unnecessary files under Areas:

Clear some files in the AspnetMvcArea. Web. Admin project:

1./files and subfolders In the Content directory

2./file and subfolders In the Scripts directory

3./Controllers files and subfolders

4. files and folders under/Views, but the web. config file is retained.

5. Delete the project Global File.

Step 3:

Uninstall the project: AspnetMvcArea. Web and AspnetMvcArea. Web. Admin

Step 4:

Modify the project file.

1. Modify the main project: AspnetMvcArea. Web

Modify the content of the AspnetMvcArea. Web. csproj file and open it in a notepad editor.

Cancel the annotation in China 1:

A.

B.

2. Modify the Sub-Project: AspnetMvcArea. Web. Admin

Same as above. removed annotation: AspnetMvcArea. Web. Admin. csproj

A.

B. Pay attention to the need here. In fact, the English is clearly written.

Save the preceding changes and exit. Return to the vs environment.

Step 5. Reload the project.

Load the two uninstalled projects.

Step 6. Add the controller and view on the sub-project.

1. Add AccountController to be the same as the default AccountController name added to the main project.

2. Add views to the actions of AccountController.

LogOn:

LogOff:

Step 7: Add a routing class library

In the solution, add a Class Library: AspnetMvcArea. MapRoutes and create a Routes static class.

Note: add the referenced version: 2.0

Inside the Routes static class:

Step 8. In the main project AspnetMvcArea. Web, add the reference of AspnetMvcArea. Web. Admin and AspnetMvcArea. MapRoutes.

Modify the Global file in the main project AspnetMvcArea. Web

Step 9: Modify the views of the AccountController in the AspnetMvcArea. Web. Admin project.

1. Index View

2. LogOn View

3. LogOff View

Step 10: complete most of the tasks. Modify a connection. For testing.

1. Modify the Views/Shared/Site. Master of the main project.

2. Modify Views/Shared/LogOnUserControl. ascx of the main project.

Step 11: complete?

Of course, if it is done, it would be perfect. Unfortunately... When running, the view cannot be found in the area.

This is a view positioning problem. You can modify the Areas Project name, assembly name, and namespace. All are the same as area names.

Change the AspnetMvcArea. Web. Admin project name, assembly name, and namespace to the same name as the defined area: Admin.

During project compilation, the Areas folder is generated under the main project. You can see the directory in the folder. This is complete.
Of course, to keep the naming rules. You can change the area name to AspnetMvcArea. Web. Admin without changing the Assembly name and namespace. That is, it is troublesome to write at the front end.
For ease of writing, you can also save the area name in a constant.
In a word, make sure that the area name is the same as that of the area project assembly. The namespaces can be inconsistent.

Ps: Modify the namespace parameters used by the MapAreaRoute method in the Routes static class synchronously.

We are looking forward to the next version.

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.