Multi-level MVC folder, application of Asp.net mvc2.0 Areas

Source: Internet
Author: User

This article is only written to people who encounter the same problem, so it will be a little short.

Problem:

There is a project to: 1. User management background; 2. developer background; 3. User foreground.

Take the article list module as an example.

Expected access path: 1. User Management Background: http://demo.com/admin/News/index

2, developers Background: http://demo.com/s7mmer/News/Index

3, user front-end: http://demo.com/News/Index

  

It is very troublesome to use Asp.net mvc1.0 to do this. I checked it online and found that Asp.net mvc2.0 provides the areas feature.

Solution:

I checked the usage on the Internet and added an areas named admin to the project. Right-click vs to open the project or the "areas" option cannot be found in the folder, I don't know if I understand the error or the patch is not installed?

Therefore, you can only take the following steps:

1. Create an areas folder in the root directory

2. manually create a directory in areas as follows:

3. Add a ing and create a class myarearegistration. CS in the areas folder.

The Code is as follows:

 

Code

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. MVC;
Using system. Web. Routing;
Namespace s7mmer. Web
{
Public class adminarearegistration: arearegistration
{
Public override string areaname
{
Get {return "admin ";}
}
Public override void registerarea (arearegistrationcontext context)
{
// You can modify matching rules by yourself.
Context. maproute (
"Admincontroller_default ",
"Admin/{controller}/{action}/{ID }",
New {controller = "home", Action = "Index", id = ""}, // parameter defaults
New String [] {"s7mmer. Web. Areas. admin. controllers"} // namespace of Controllers
);
}
}
Public class s7mmerarearegistration: arearegistration
{
Public override string areaname
{
Get {return "s7mmer ";}
}
Public override void registerarea (arearegistrationcontext context)
{
// You can modify matching rules by yourself.
Context. maproute (
"S7mmercontroller_default ",
"S7mmer/{controller}/{action}/{ID }",
New {controller = "home", Action = "Index", id = ""}, // parameter defaults
New String [] {"s7mmer. Web. Areas. s7mmer. controllers"} // namespace of Controllers
);
}
}

Public class websitearearegistration: arearegistration
{
Public override string areaname
{
Get {return "website ";}
}
Public override void registerarea (arearegistrationcontext context)
{
// You can modify matching rules by yourself.
Context. maproute (
"Websitecontroller_default ",
"{Controller}/{action}/{ID }",
New {controller = "home", Action = "Index", id = ""}, // parameter defaults
New String [] {"s7mmer. Web. Areas. Website. controllers"} // namespace of Controllers
);
}
}

}

 

4. Create newscontroller. CS in the admin folder under the areas folder.

5. Right-click public actionresult index () in newscontroller. CS to add a view.

Index. aspx has been added to news

6. Modify the global. asax file in the root directory and add arearegistration. registerallareas () in application_start ();

 

Protected void application_start ()
{
Arearegistration. registerallareas ();
// Registerroutes (routetable. routes );
}

 

 

Compile and access http: // localhost: 1108/admin/news/index. User Management Background access is successful!

The other results can be achieved by following the above steps, so that the front and back-end files can be separated to facilitate management.

 

Sailing against the water, leaving without entering

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.