2016-08-07 9 views
0

을 설명하는 몇 가지 자습서를 읽고을보기 폴더를 이동해야하는 경우보기 폴더의 기본 경로로 바꿉니다. 그러나, 나는 어떻게 보기 엔진에 의해 검색되는 경로를 추가 알아 내려고 노력했습니다. 여기 ASP.NET Core의 검색 위치 추가

내가 지금까지이 작업은 다음과 같습니다

public class BetterViewEngine : IViewLocationExpander 
{ 
    public void PopulateValues(ViewLocationExpanderContext context) 
    { 
    } 

    public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations) 
    { 
     return viewLocations.Select(s => s.Add("")); //Formerly s.Replace("oldPath", "newPath" but I wish to add 
    } 
} 

그리고 내 Startup.cs

services.AddMvc().AddRazorOptions(options => 
     { 
      options.ViewLocationExpanders.Add(new BetterViewEngine()); 
     }); 
+0

을 보기의 기본 위치? –

+0

이 게시물을 참조하십시오 : [ASP.NET MVC에서 "보기를 검색"하기 위해 사용자 지정 위치를 지정할 수 있습니까?] (http://stackoverflow.com/questions/632964/can-i-specify-a-custom-location-to -search-for-views-in-asp-net-mvc) –

답변

1

에 당신의 의견을 검색하기위한 기본 동작을 변경하려는 경우,이 시도 :

public class BetterViewEngine : IViewLocationExpander 
{ 
    public void PopulateValues(ViewLocationExpanderContext context) 
    { 
     context.Values["customviewlocation"] = nameof(BetterViewEngine); 
    } 

    public IEnumerable<string> ExpandViewLocations(
     ViewLocationExpanderContext context, IEnumerable<string> viewLocations) 
    { 
     return new[] 
     { 
      "/folderName/{1}/{0}.cshtml", 
      "/folderName/Shared/{0}.cshtml" 
     }; 
    } 
} 

하지만 폴더 중 하나의 이름을 바꾸려면 이 시도 :

public IEnumerable<string> ExpandViewLocations(
     ViewLocationExpanderContext context, IEnumerable<string> viewLocations) 
{ 

     // Swap /Shared/ for /_Shared/ 
     return viewLocations.Select(f => f.Replace("/Shared/", "/_Shared/")); 

} 
+0

기본 위치를 변경하려고하지 않았습니다. 방금 엔진이 기본이 아닌 다른 장소를 검색해야했습니다. 두 경우 모두 예제가 작동합니다. 감사합니다 –

+0

어떻게 뷰 경로에 추가 정보를 전달할 수 있습니까? 예를 들어 다음 경로를 갖고 싶습니다. "/folderName/{1}/{0}.{2}.cshtml"{2}는 현재 문화권 (예 : en-US)의 자리 표시 자입니까? 내 문제는 그것이 존재하지 않으면 "localized"뷰 (/folderName/Home/Index.en-US.cshtml)를 반환하거나 그렇지 않으면 기본 뷰 (/folderName/Home/Index.cshtml)를 반환하려는 것입니다. 이 사건에 대한 간단한 해결책이 있습니까? 감사. – Laserson

0

이는 그의 대답의 첫 번째 부분 읽은 후 내가 할 필요가 무엇인지 알아 내기 위해 나에게 분했다 때문에 단지 Sirwan의 대답에 확대 : 당신은 변경할

public class ViewLocationRemapper : IViewLocationExpander 
{ 
    public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations) 
    { 
     return new[] 
     { 
      "/Views/{1}/{0}.cshtml", 
      "/Views/Shared/{0}.cshtml", 
      "/Views/" + context.Values["admin"] + "/{1}/{0}.cshtml" 
     }; 
    } 

    public void PopulateValues(ViewLocationExpanderContext context) 
    { 
     context.Values["admin"] = "AdminViews"; 
    } 
}