MVC ASP.NET MVC5 using Area

MVC ASP.NET MVC5 using Area

1. Why use area?

     In a large-scale ASP.NET mvc5 project, there are generally many functional modules. These functional modules can be separated by Area (translated into area in Chinese), such as: Admin, Customer, Bill. In the ASP.NET MVC project, after the various functions are divided into different Areas, each Area has an independent Controller and View file structure. In this way, these functions can be assigned to different developers to develop at the same time without conflicting with each other. Such a file structure performs its own duties, is intuitive and clear, and is easy to maintain and manage. Let's take a look at how to create a link between Area and Area in ASP.NET MVC5.         (Mvc 2.0 version introduced area.)

2. Arrangement:

Let's see how this Area works in the project.

Let's look at the code of Global.asax.cs:

  public class Global : HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {
            AreaRegistration.RegisterAllAreas();
            BootStrapper.ConfigureDependencies();
            GlobalConfiguration.Configure(WebApiConfig.Register);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }
    }

 The first line of code: AreaRegistration.RegisterAllAreas(); is to register all areas in the ASP.NET MVC application. Each area has its own independent Controller, View file structure and routing configuration. We can completely regard the area of ​​ASP.NET MVC as an ASP.NET MVC project.

Each area in ASP.NET MVC5 has a class: area name + AreaRegistration, such as the BaseManage area:

public class BaseManageAreaRegistration : AreaRegistration 
    {
        public  override  string AreaName
        {
            get 
            {
                return "BaseManage";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context) 
        {
            context.MapRoute(
                "BaseManage_default",
                "BaseManage/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }
            );
        }
    }

The role of this class is mainly used to indicate the area name and the route of the area Controller. You can see that the routing rule is "BaseManage/{controller}/{action}/{id}", which is followed by a BaseManage.

3. How to handle the link jump of Action between views in the Area?

1. Jump in the same area:

@Html.ActionLink("Click me", "About")

2. Jump in different areas:

@Html.ActionLink("Click me", "About",new{ area ="Support" )

3. Adjust to no area:

@Html.ActionLink("Click me", "About",new{ area ="")

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326173857&siteId=291194637