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"; } }
原文地址:https://www.cnblogs.com/layla/p/9235616.html
时间: 2024-10-10 14:43:38