/// <summary> /// 构造区域路由 /// </summary> /// <param name="currentArea">当前区域枚举</param> /// <param name="customAreaName">自定义区域名称</param> protected void AreaRegistration(AreaRoute currentArea, string customAreaName = null) { CurrentArea = currentArea; CustomAreaName = customAreaName; if (string.IsNullOrWhiteSpace(CustomAreaName)) { CurrentRouteName = string.Format("{0}_Default", AreaName); CurrentRoutePath = MvcApplication.MainAreaRoute == currentArea ? "" : AreaName + "/"; CustomAreaName = CurrentArea.ToString(); } else { CurrentRouteName = string.Format("{0}_{1}_Default", AreaName, CustomAreaName); CurrentRoutePath = CustomAreaName + "/"; } }
/// <summary> /// 注册主路由 /// </summary> /// <remarks>最低优先级</remarks> public virtual void RegisterRoutes(RouteCollection routes) { switch (MainAreaRoute) { //默认路由 case AreaRoute.None: routes.MapRoute( name: "Default", // 路由名称 url: "{controller}/{action}/{id}", // 带有参数的 URL defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值 , namespaces: new string[] { "YSWL.Web.Controllers" } ).DataTokens.Add("area", "None"); break; //区域主路由 default: string area = MainAreaRoute.ToString(); routes.MapRoute( name: "Default", url: "{controller}/{action}/{viewname}/{id}", defaults: new { area = area, controller = "Home", action = "Index", viewname = UrlParameter.Optional, id = UrlParameter.Optional }, constraints: new { viewname = @"^[A-Za-z_]+${0,50}", //大小写字母/下划线 id = @"[\d]{0,11}" //*表示数字长度11 //new { id = @"\d*" } 长度不限 } , namespaces: new[] { string.Format("YSWL.Web.Areas.{0}.Controllers", area) } ).DataTokens.Add("area", area); routes.MapRoute( name: "Default_Base", url: "{controller}/{action}/{id}", defaults: new { area = area, controller = "Home", action = "Index", id = UrlParameter.Optional } , namespaces: new[] { string.Format("YSWL.Web.Areas.{0}.Controllers", area) } ).DataTokens.Add("area", area); break; } }
/// <summary> /// 当前系统是否有指定区域 /// </summary> /// <remarks>在运行池启动时加载目录</remarks> /// <param name="area">Area</param> public static bool HasArea(AreaRoute area) { if (area == AreaRoute.None) { return(false); } if (HttpContext.Current == null) { throw new ArgumentNullException(); } if (AreaList.Count < 1) { #region 加载区域目录 string mapPath = HttpContext.Current.Server.MapPath("/Areas"); if (!System.IO.Directory.Exists(mapPath)) { return(false); } System.IO.DirectoryInfo directoryInfo = new System.IO.DirectoryInfo(mapPath); System.IO.DirectoryInfo[] subInfos = directoryInfo.GetDirectories(); if (subInfos.Length < 1) { return(false); } foreach (System.IO.DirectoryInfo item in subInfos) { AreaList.Add(item.Name); } #endregion } return(AreaList.Contains(area.ToString())); }
private ViewEngineResult FindView(AreaRoute areaRoute, ControllerContext controllerContext, string viewName, bool useCache, bool isPartialView, string masterName = null) { // Get the name of the controller from the path string controller = controllerContext.RouteData.Values["controller"].ToString(); string controllerspaces = controllerContext.RouteData.DataTokens["Space"] as string; string viewSpaces = string.Empty; //检测命名空间, 识别第二代MVC结构 //if (namespaces != null && namespaces.Length > 0) //{ // foreach (string ns in namespaces) // { // if (!ns.EndsWith(".*") && !ns.EndsWith(".Controllers")) // { // controllerFolder = ns.Substring(ns.LastIndexOf('.') + 1); // break; // } // } //} if (!string.IsNullOrWhiteSpace(controllerspaces)) { viewSpaces = controllerspaces; } string area = areaRoute.ToString(); //DONE: 非空时标识使用自定义引擎 子引擎的缓存差异标识 注册子引擎路由时追加, 此参数在回调时回传 string tmpTag = SubAreaViewEngine.SubEngineMap.ContainsKey(areaRoute) ? SubAreaViewEngine.GetTagString(areaRoute, controllerContext) : string.Empty; // Create the key for caching purposes string keyPath = System.IO.Path.Combine(area, controller, MainThemeName, viewSpaces, viewName, tmpTag); // Try the cache if (useCache) { //If using the cache, check to see if the location is cached. string cacheLocation = ViewLocationCache.GetViewLocation(controllerContext.HttpContext, keyPath); if (!string.IsNullOrWhiteSpace(cacheLocation)) { return(isPartialView ? new ViewEngineResult(CreatePartialView(controllerContext, cacheLocation), this) : new ViewEngineResult(CreateView(controllerContext, cacheLocation, masterName), this)); } } // Remember the attempted paths, if not found display the attempted paths in the error message. var attempts = new List <string>(); // Check ViewName 是否是具体路径 具体路径时, 不再以FormatViewPath方式查找 bool isSpecificPath = viewName.StartsWith("~") || viewName.StartsWith("/"); if (isSpecificPath) { if (FileExists(controllerContext, viewName)) { ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, keyPath, viewName); return(isPartialView ? new ViewEngineResult(CreatePartialView(controllerContext, viewName), this) : new ViewEngineResult(CreateView(controllerContext, viewName, masterName), this)); } // If not found, add to the list of attempts. attempts.Add(viewName); } else { string[] locationFormats; if (areaRoute == AreaRoute.None) { locationFormats = ViewLocationFormats; } else if (SubAreaViewEngine.SubEngineMap.ContainsKey(areaRoute) && !string.IsNullOrWhiteSpace(tmpTag)) { locationFormats = SubAreaViewEngine.SubEngineMap[areaRoute](controllerContext, BaseAreaViewLocationFormat, tmpTag); } else if (SubAreaViewEngine.SubEngineMap.ContainsKey(areaRoute)) { locationFormats = SubAreaViewEngine.SubEngineMap[areaRoute](controllerContext, BaseAreaViewLocationFormat, string.Empty); } else { locationFormats = AreaViewLocationFormats; } // for each of the paths defined, format the string and see if that path exists. When found, cache it. foreach (string rootPath in locationFormats) { string currentPath; if (areaRoute == AreaRoute.None) { //默认模版路径 currentPath = string.Format(rootPath, viewName, controller); } else if (string.IsNullOrWhiteSpace(viewSpaces)) { //第一代MVC(默认)路径 currentPath = string.Format(rootPath, viewName, controller, area); } else { //第二代MVC路径 currentPath = string.Format(rootPath, controller + "/" + viewName, viewSpaces, area); } if (FileExists(controllerContext, currentPath)) { ViewLocationCache.InsertViewLocation(controllerContext.HttpContext, keyPath, currentPath); return(isPartialView ? new ViewEngineResult(CreatePartialView(controllerContext, currentPath), this) : new ViewEngineResult(CreateView(controllerContext, currentPath, masterName), this)); } // If not found, add to the list of attempts. attempts.Add(currentPath); } } // if not found by now, simply return the attempted paths. return(new ViewEngineResult(attempts.Distinct().ToList())); }
protected static string PathUploadFolderBase(AreaRoute currentArea) { return string.Format("/{0}/{1}/", Maticsoft.Components.MvcApplication.UploadFolder, currentArea.ToString()); }