2013-09-07 3 views
0

런타임에서 영역 디렉토리에서 영역을 검색하고 등록하는 플러그 가능한 ASP.NET MVC 웹 응용 프로그램에서 작업하고 있습니다.플러그 가능한 영역에 대한 면도기를 찾을 수 없음

제 문제는 뷰 엔진이 해당 영역의 뷰를 찾지 못한다는 것입니다.

: 나는 다음과 같은 오류가
Html.Partial("_SearchBarPartial") 

을 실행할 때

htmlHelper.ViewContext.RouteData.DataTokens["area"] = "Catalog"; 

을하지만 :

나는 하나의 부분보기는

~/Areas/Catalog/Views/Shared/_SearchBarPartial.cshtml

내가 사용 지역을 설정

에 위치하고있다

The partial view '_SearchBarPartial' was not found or no view engine supports the searched locations. The following locations were searched:

~/Areas/Catalog/Views/Home/_SearchBarPartial.aspx ~/Areas/Catalog/Views/Home/_SearchBarPartial.ascx ~/Areas/Catalog/Views/Shared/_SearchBarPartial.aspx ~/Areas/Catalog/Views/Shared/_SearchBarPartial.ascx ~/Views/Home/_SearchBarPartial.aspx ~/Views/Home/_SearchBarPartial.ascx ~/Views/Shared/_SearchBarPartial.aspx ~/Views/Shared/_SearchBarPartial.ascx ~/Areas/Catalog/Views/Home/_SearchBarPartial.cshtml ~/Areas/Catalog/Views/Home/_SearchBarPartial.vbhtml ~/Areas/Catalog/Views/Shared/_SearchBarPartial.cshtml ~/Areas/Catalog/Views/Shared/_SearchBarPartial.vbhtml ~/Views/Home/_SearchBarPartial.cshtml ~/Views/Home/_SearchBarPartial.vbhtml ~/Views/Shared/_SearchBarPartial.cshtml ~/Views/Shared/_SearchBarPartial.vbhtml

내 폴더 구조는 다음과 같습니다

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(Bootstrapper), "LoadAreas")] 

    public class Bootstrapper 
    { 

     public static string ModuleDirectory 
     { 
      get { return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Areas"); } 
     } 

     public static void LoadAreas() 
     { 
      var assemblyFiles = Directory.GetFiles(ModuleDirectory, "*.dll", SearchOption.AllDirectories); 

      assemblyFiles.ForEach(af => 
      { 
       var assembly = Assembly.LoadFile(af); 
       if(assembly.GetTypes().Any(t => IsArea(t)) 
       { 
        BuildManager.AddReferencedAssembly(assembly); 
       } 
      }); 
     } 

     private static bool IsArea(Type t) 
     { 
      return typeof (AreaRegistration).IsAssignableFrom(t) && !t.IsAbstract; 
     } 
    } 

Global.asax.cs

public class MvcApplication : System.Web.HttpApplication 
{ 
    protected void Application_Start() 
    { 
     AreaRegistration.RegisterAllAreas(); 
     // ... 
    } 
} 

답변

1

당신은 할 수 있습니다

bin/ 
Areas/ 
    Catalog/ 
     bin/ 
      CatalogArea.dll 
     Content/ 
     Scripts/ 
     Views/ 
      Shared/ 
       _SearchBarPartial.cshtml 
     CatalogArea.cs 
Content/ 
Scrips/ 
Views/ 
Global.asax 

내가 응용 프로그램을 시작하기 전에 실행 된 다음 코드를 사용 영역을 등록하려면 Global.asax.cs에 사용자 정의 RazorViewEngine을 추가하십시오. 예 :

RazorViewEngine objRazorViewEngine = new RazorViewEngine(); 
objRazorViewEngine.PartialViewLocationFormats = new string[] 
{ 
"~/Areas/Catalog/Views/Shared/{0}.cshtml" 
}; 
ViewEngines.Engines.Add(objRazorViewEngine);