2011-03-21 3 views
18

영역을 사용하여 MVC3 솔루션을 설정하려고하지만 다른 어셈블리에 내 영역을 갖고 싶습니다. 예를 들어, 마스터 페이지, 스타일 시트, 스크립트, 로그인 페이지 등과 같은 공유 리소스가 포함 된 부모 어셈블리가 필요합니다.하지만 별도의 어셈블리에서 서로 다른 비즈니스 기능 영역을 원합니다.ASP.NET MVC3 - 별도 어셈블리의 영역

MVC2 미리보기 용으로 작성된이 샘플을 시도했습니다 : http://msdn.microsoft.com/en-us/library/ee307987%28VS.100%29.aspx. (참고,이 원래 스택 오버플로 스레드에서 발견 : ASP.NET MVC - separating large app). 그러나 MVC3에는 뷰 파일을 주 프로젝트로 옮길 수있는 옵션이없는 것으로 보입니다. 나는 임베디드 리소스/VirtualPathProvider 옵션을 사용하는 것에 열중하지 않습니다.

MVC3에서이 작업을 수행하는 방법에 대한 제안 사항이 있으십니까?

감사합니다, 건너 뛰기

+0

이 게시물은 아마도 당신을위한 더 나은 솔루션입니다 : http://stackoverflow.com/questions/4241399/asp-net-mvc-3-rc-arearegistration-registerallareas-and-dynamically-loaded-assem – gidmanma

답변

2

당신은 지역의 사용없이 컨트롤러와 뷰를 분리 할 수 ​​있습니다. 컨트롤러의 경우 Windsor 또는 다른 IoC 컨테이너를 사용하여 다른 어셈블리의 컨트롤러를 확인할 수 있습니다. 예를 들어, 당신은이 방법으로 모든 컨트롤러를 등록 할 수 있습니다 :

container.Register(AllTypes.FromAssemblyInDirectory(new AssemblyFilter(HttpRuntime.BinDirectory)).BasedOn<IController>().Configure(c => c.LifeStyle.Transient)); 

은 또한 당신은 IDependencyResolver 다음 DependencyResolver.SetResolver (...)를 설정 구현해야합니다.

    빌드 후 적절한 위치에
  1. 임베디드 자원과 VirtualPathProvider
  2. 간단한 복사 뷰 파일을/(

우리는 간단한 프레임 워크를 구축 배포 :보기에

두 가지 옵션이 있습니다 Portable Areas와 유사)에 Windsor와 VirutalPathProvider 구현에 의해 제공되는 임베디드 리소스 뷰를 사용합니다.

+0

이것은 우연히 GitHub에 코드? – cecilphillip

+0

죄송하지만 이전 직원이 소유하고있는 코드를 공유 할 수 없습니다. 결론적으로 우리는 임베디드 뷰를 삭제했지만 컨트롤러는 별도의 어셈블리에 남아있게되었습니다. 이 솔루션의 특정 부분에 관심이 있습니까? –

+0

임베디드 뷰 및 virtualPathProvider – cecilphillip

2

MvcContrib with Portable Areas을 사용할 수 있지만 이렇게하면보기가 포함됩니다.

MVC 및 클래스 라이브러리 프로젝트 만 만들면됩니다. 영역에서 클래스 라이브러리로 뷰를 제외한 모든 작업을 마친 후 MVC 프로젝트에서 영역을 만듭니다.

NuGet을 사용하면 모든 MVC 프로젝트에서 새 NuGet 영역을 사용할 수 있습니다.

11

1 - 당신의 어셈블리 정보이 추가 - differrent MVC 프로젝트에 별도의 당신 MVC 영역은 자신의 별도의 어셈블리

2로 컴파일합니다.CS 클래스, 메소드를 호출하는 응용 프로그램이

[assembly: PreApplicationStartMethod(typeof(PluginAreaBootstrapper), "Init")] 

3로드 될 때 - 여기서 초기화 방법이 부하

public class PluginAreaBootstrapper 
{ 
    public static readonly List<Assembly> PluginAssemblies = new List<Assembly>(); 

    public static List<string> PluginNames() 
    { 
     return PluginAssemblies.Select(
      pluginAssembly => pluginAssembly.GetName().Name) 
      .ToList(); 
    } 

    public static void Init() 
    { 
     var fullPluginPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Areas"); 

     foreach (var file in Directory.EnumerateFiles(fullPluginPath, "*Plugin*.dll")) 
      PluginAssemblies.Add(Assembly.LoadFile(file)); 

     PluginAssemblies.ForEach(BuildManager.AddReferencedAssembly); 
    } 
} 

4시 호출 할 때의 모습입니다 - 사용자 정의 RazorViewEngine 추가

public class PluginRazorViewEngine : RazorViewEngine 
{ 
    public PluginRazorViewEngine() 
    { 
     AreaMasterLocationFormats = new[] 
     { 
      "~/Areas/{2}/Views/{1}/{0}.cshtml", 
      "~/Areas/{2}/Views/{1}/{0}.vbhtml", 
      "~/Areas/{2}/Views/Shared/{0}.cshtml", 
      "~/Areas/{2}/Views/Shared/{0}.vbhtml" 
     }; 

     AreaPartialViewLocationFormats = new[] 
     { 
      "~/Areas/{2}/Views/{1}/{0}.cshtml", 
      "~/Areas/{2}/Views/{1}/{0}.vbhtml", 
      "~/Areas/{2}/Views/Shared/{0}.cshtml", 
      "~/Areas/{2}/Views/Shared/{0}.vbhtml" 
     }; 

     var areaViewAndPartialViewLocationFormats = new List<string> 
     { 
      "~/Areas/{2}/Views/{1}/{0}.cshtml", 
      "~/Areas/{2}/Views/{1}/{0}.vbhtml", 
      "~/Areas/{2}/Views/Shared/{0}.cshtml", 
      "~/Areas/{2}/Views/Shared/{0}.vbhtml" 
     }; 

     var partialViewLocationFormats = new List<string> 
     { 
      "~/Views/{1}/{0}.cshtml", 
      "~/Views/{1}/{0}.vbhtml", 
      "~/Views/Shared/{0}.cshtml", 
      "~/Views/Shared/{0}.vbhtml" 
     }; 

     var masterLocationFormats = new List<string> 
     { 
      "~/Views/{1}/{0}.cshtml", 
      "~/Views/{1}/{0}.vbhtml", 
      "~/Views/Shared/{0}.cshtml", 
      "~/Views/Shared/{0}.vbhtml" 
     }; 

     foreach (var plugin in PluginAreaBootstrapper.PluginNames()) 
     { 
      masterLocationFormats.Add(
       "~/Areas/" + plugin + "/Views/{1}/{0}.cshtml"); 
      masterLocationFormats.Add(
       "~/Areas/" + plugin + "/Views/{1}/{0}.vbhtml"); 
      masterLocationFormats.Add(
       "~/Areas/" + plugin + "/Views/Shared/{1}/{0}.cshtml"); 
      masterLocationFormats.Add(
       "~/Areas/" + plugin + "/Views/Shared/{1}/{0}.vbhtml"); 

      partialViewLocationFormats.Add(
       "~/Areas/" + plugin + "/Views/{1}/{0}.cshtml"); 
      partialViewLocationFormats.Add(
       "~/Areas/" + plugin + "/Views/{1}/{0}.vbhtml"); 
      partialViewLocationFormats.Add(
       "~/Areas/" + plugin + "/Views/Shared/{0}.cshtml"); 
      partialViewLocationFormats.Add(
       "~/Areas/" + plugin + "/Views/Shared/{0}.vbhtml"); 

      areaViewAndPartialViewLocationFormats.Add(
       "~/Areas/" + plugin + "/Views/{1}/{0}.cshtml"); 
      areaViewAndPartialViewLocationFormats.Add(
       "~/Areas/" + plugin + "/Views/{1}/{0}.vbhtml"); 
      areaViewAndPartialViewLocationFormats.Add(
       "~/Areas/" + plugin + "/Areas/{2}/Views/{1}/{0}.cshtml"); 
      areaViewAndPartialViewLocationFormats.Add(
       "~/Areas/" + plugin + "/Areas/{2}/Views/{1}/{0}.vbhtml"); 
      areaViewAndPartialViewLocationFormats.Add(
       "~/Areas/" + plugin + "/Areas/{2}/Views/Shared/{0}.cshtml"); 
      areaViewAndPartialViewLocationFormats.Add(
       "~/Areas/" + plugin + "/Areas/{2}/Views/Shared/{0}.vbhtml"); 
     } 

     ViewLocationFormats = partialViewLocationFormats.ToArray(); 
     MasterLocationFormats = masterLocationFormats.ToArray(); 
     PartialViewLocationFormats = partialViewLocationFormats.ToArray(); 
     AreaPartialViewLocationFormats = areaViewAndPartialViewLocationFormats.ToArray(); 
     AreaViewLocationFormats = areaViewAndPartialViewLocationFormats.ToArray(); 
    } 
} 

5 - 당신의 다른 MVC (지역)에서 지역을 등록

namespace MvcApplication8.Web.MyPlugin1 
{ 
    public class MyPlugin1AreaRegistration : AreaRegistration 
    { 
     public override string AreaName 
     { 
      get { return "MyPlugin1"; } 
     } 

     public override void RegisterArea(AreaRegistrationContext context) 
     { 
      context.MapRoute(
       "MyPlugin1_default", 
       "MyPlugin1/{controller}/{action}/{id}", 
       new {action = "Index", id = UrlParameter.Optional} 
       ); 
     } 
    } 
} 
프로젝트 관리

소스 코드 및 추가 참조는 여기에서 찾을 수 있습니다 할 수 있습니다 http://blog.longle.io/2012/03/29/building-a-composite-mvc3-application-with-pluggable-areas/

+0

이 페이지가 aspx 페이지에서도 작동하는지 알려주세요. – Saravanan

0

다른 MVC 응용 프로그램 내에서 지역으로 작동하는 프로젝트를 만드는 방법에 대한 this article를 참조하십시오. 기본적으로 영역 프로젝트의 파일은 주 프로젝트의 Area 폴더 아래에 있지만 주 프로젝트의 일부로 포함되지 않습니다 (프로젝트 파일에서 참조되지 않음).