C# Area區域配置,修改默認路由
阿新 • • 發佈:2018-06-27
mapr var esp map chan default com col public
1.右鍵項目新建文件夾 Areas
2.先把項目分類包好,建兩個文件夾,放Controller和View,Model也可以放在這裏
因為項目啟動默認打開的是Home/Index ,我把它放在了Website文件夾內了,這就需要更改路由配置了
3.如果更改了默認目錄,就要去修改路由配置了,打開Global.asax.cs代碼如下,F12進 RouteConfig
using System.Web; using System.Web.Mvc; using System.Web.Routing; namespace Demo.Web { publicclass MvcApplication : HttpApplication { protected void Application_Start() { // 移除X-AspnetMvc-Version HTTP 開頭 MvcHandler.DisableMvcResponseHeader = true; // 註冊所有Area AreaRegistration.RegisterAllAreas(); RouteConfig.RegisterRoutes(RouteTable.Routes); AutofacConfig.Register(); PermissionUtil.ValidPermissions(); } } }
4.修改RouteConfig,主要修改就是加了 namespaces: new[] { "Demo.Web.Areas.Website.Controllers" } 和 route.DataTokens["area"] = "Website";
using System.Web.Mvc; using System.Web.Routing; namespace AnFund.Web { public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); var route = routes.MapRoute("Default", "{controller}/{action}/{id}", new {controller = "Home", action = "Index", id = UrlParameter.Optional }, namespaces: new[] { "Demo.Web.Areas.Website.Controllers" } ); // 更改視圖默認位置 route.DataTokens["area"] = "Website"; } }
C# Area區域配置,修改默認路由