C# Area區域配置,修改默認路由


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
{
    public class 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";
        }
    }

 

 

來源於網絡


免責聲明!

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



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