ASP.NET MVC Area操作


ASP.NET MVC Area操作

 *     1、新建 Area:右鍵 -> Add -> Area...
 *     2、繼承 AreaRegistration,配置對應此 Area 的路由
 *     3、在 Global 中通過 AreaRegistration.RegisterAllAreas(); 注冊此 Area
 *     4、有了 Area,就一定要配置路由的命名空間

 

using System.Web.Mvc;

 

namespace MVC20.Areas.AsynchronousController

{

    // 新建一個 Area 會自動生成這個繼承自 AreaRegistration 的類

    // 如果需要使用此 Area 下的 MVC, 需要在 Global 中 AreaRegistration.RegisterAllAreas();

    public class AsynchronousControllerAreaRegistration : AreaRegistration

    {

        public override string AreaName

        {

            get

            {

                return "AsynchronousController";

            }

        }

        public override void RegisterArea(AreaRegistrationContext context)

        {

            // 在 Area 中配置路由的時候,要設置命名空間(即本例中的第 4 個參數)

            context.MapRoute(

                "AsynchronousController_default",

                "AsynchronousController/{controller}/{action}/{id}",

                new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults

                new string[] { "MVC20.Areas.AsynchronousController.Controllers" }

            );

        }

    }

}

 

Global.asax

代碼

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using System.Web.Routing;

 

namespace MVC20

{

    public class MvcApplication : System.Web.HttpApplication

    {

        public static void RegisterRoutes(RouteCollection routes)

        {

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

 

            // 用於本項目中使用了 Area,所以在配置路由的時候,要設置命名空間(即本例中的第 4 個參數)

            routes.MapRoute(

                "Default", // Route name

                "{controller}/{action}/{id}", // URL with parameters

                new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // UrlParameter.Optional - 如果從url路由中無法獲取某個參數的值,則從url參數中獲取該參數的值

                new string[] {"MVC20.Controllers"}

            );

        }

 

        protected void Application_Start()

        {

            // 注冊本應用程序中的所有 Area

            AreaRegistration.RegisterAllAreas();

 

            RegisterRoutes(RouteTable.Routes);

        }

    }

}

'mso-�-o83���0.5pt; font-family:"Segoe UI","sans-serif";color:black;background:#DDEDFB'> false;  

 

       } 

       return true;  

        }   

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM