2014-02-10 3 views
1

내 솔루션은 깊은 폴더 구조, 사업 구조와 일치하는 깊이 3 ~ 4 레벨을 가고, 예를 들면 :ASP.NET MVC에서 심오한 비즈니스 구조를 적용하기위한 옵션은 무엇입니까?

\Franchises 
    \Regionals 
     \Billing 
     \Monthly 
     \... 
     \... 
     \... 
     \... 
    \... 
    \... 
\... 
\... 

도메인의 폴더 구조를 가지고, 프로세스 및 보고서 프로젝트는이 구조와 일치한다.

MVC는 통증입니다. 기본적으로는 단 1 개 깊은 수준의 수 :

\Areas\Franchises\Controllers\BlahBlah 

이 아무데도 반영 할만큼 가까이 : MVC 영역을 사용하여

\Controllers\BlahBlah 

을 나는 2 개 수준 깊은 (klutzily 경로에 두 개의 폴더를 더 추가)를 얻을 수 있습니다 사업의 깊은 구조.

WebUI를 여러 프로젝트로 세로로 분할하는 것을 주저하고 있습니다. 통합 작업이 더 필요하기 때문에 비즈니스 구조를 과도하게 과장하는 것처럼 보입니다.

MVC 프로젝트에 임의의 폴더 레벨을 부과하는 방법이 있습니까? 컨트롤러의 모든 라우팅을 수동으로 하드 코딩해야합니까?

+0

나는 이런 식으로 뭔가 함께 갈 것입니다, http://mvccoderouting.codeplex.com –

답변

2

모든 컨트롤러 physycal 폴더를 라우팅 할 필요는 없습니다. 원하는대로 폴더 구조를 구성 할 수 있지만 모든 트리 하위 구조에 경로를 자동으로 매핑하려면 Controllers 폴더의 하위 네임 스페이스를 탐색하고 자동으로 매핑 할 수 있습니다 어떤 하드 코드하지 않고 당신이 루트로는, 내 해결책이 :

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Reflection; 
using System.Web.Mvc; 
using System.Web.Routing; 

namespace WebApplication2 
{ 
    public class RouteConfig 
    { 
     public static void RegisterRoutes(RouteCollection routes) 
     { 
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
      routes.MapRoute(
       name: "Default", 
       url: "{controller}/{action}/{id}", 
       defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
      ); 
      routes.MapAllRoutesToFolders(); 
     } 
    } 

    public static class RouteExtensions 
    { 
     public static void MapAllRoutesToFolders(this RouteCollection routes) 
     { 
      const string commonControllerUrl = "{controller}/{action}/{id}"; 
      var folderMappings = GetSubClasses<Controller>() 
       .Select(x => new 
       { 
        Path = string.Join("/", x.FullName 
         .Split(".".ToCharArray(), StringSplitOptions.RemoveEmptyEntries) 
         .SkipWhile(c => c != "Controllers")) 
         .TrimStart("/Controllers/".ToCharArray()) 
         .Replace(x.Name, string.Empty), 
        Name = x.Name.Replace("Controller", string.Empty) 
       }); 
      var controllerPaths = 
       folderMappings.Where(s => !string.IsNullOrEmpty(s.Path)) 
        .Select(x => new 
        { 
         ControllerName = x.Name, 
         Path = x.Path + commonControllerUrl 
        }); 
      foreach (var controllerPath in controllerPaths) 
      { 
       routes.MapRoute(null, controllerPath.Path, new { controller = controllerPath.ControllerName, action = "Index", id = UrlParameter.Optional }); 
      } 
     } 

     private static IEnumerable<Type> GetSubClasses<T>() 
     { 
      return Assembly.GetCallingAssembly().GetTypes().Where(
       type => type.IsSubclassOf(typeof(T))).ToList(); 
     } 
    } 
} 

이 경우, 폴더 구조는 다음과 같이해야합니다 :

enter image description here

그 후에 당신은 당신의 브라우저에서 확인할 수 있습니다

enter image description here

+0

건배를 참조하십시오. –