C# Area区域配置,修改默认路由

1.右键项目新建文件夹 Areas网络

 

 

2.先把项目分类包好,建两个文件夹,放Controller和View,Model也能够放在这里spa

技术分享图片技术分享图片

 


由于项目启动默认打开的是Home/Index ,我把它放在了Website文件夹内了,这就须要更改路由配置了code

3.若是更改了默认目录,就要去修改路由配置了,打开Global.asax.cs代码以下,F12进 RouteConfigblog

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";
        }
    }

 

 

来源于网络路由

相关文章
相关标签/搜索