ASP.NET Core MVC 는 IViewLocationExpander 를 통 해 보기 검색 경 로 를 확장 합 니 다.

IViewLocationExpander API
  • ExpandView Locations Razor 보기 경 로 를 확장 하면 보기 엔진 이 이 경 로 를 검색 합 니 다
  • PopulateValues 는 호출 할 때마다 경로 가 채 워 집 니 다
  • 프로젝트 디 렉 터 리 는 다음 과 같다.

    지역 확장 기 를 만 듭 니 다.사실 저 는 여러 지역 이 필요 하지 않 습 니 다.저 는 현재 한 지역 에 여러 개의 폴 더 가 있어 서 제 보 기 를 저장 해 야 합 니 다.
    그래서 저 는 IViewLocationExpander 를 실현 하여 확장 을 통 해 사용자 정의 보기 경로 규칙 을 추가 하면 다음 과 같은 코드 세 션 을 만 들 수 있 습 니 다.
    
     public class MyViewLocationExpander : IViewLocationExpander
      {
        public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
        {
          if (context.ControllerName != null && context.ControllerName.StartsWith("App"))
          {
            viewLocations = viewLocations.Concat(
              new[] { $"/Areas/sysManage/Views/App/{context.ControllerName}/{context.ViewName}{RazorViewEngine.ViewExtension}"
                  });
            return viewLocations;
          }
    
          if (context.AreaName != "sysManage") return viewLocations;
          viewLocations = viewLocations.Concat(
            new[] { $"/Areas/sysManage/Views/System/{context.ControllerName}/{context.ViewName}{RazorViewEngine.ViewExtension}"
            });
          return viewLocations;
        }
    
        public void PopulateValues(ViewLocationExpanderContext context)
        {
        }
      }
    Startup.Configure Services 에 등록 하기
    
     public void ConfigureServices(IServiceCollection services) 
        { 
          services.Configure<RazorViewEngineOptions>(o => { 
            o.ViewLocationExpanders.Add(new MyViewLocationExpander()); 
          }); 
          services.AddMvc(); 
        } 
    
     app.UseEndpoints(endpoints =>
          {
            endpoints.MapRazorPages();
            endpoints.MapAreaControllerRoute(
              name: "sysManage", "sysManage",
              pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
          });
    최종 경로 가 가리 키 는 것 은 역시
    
    /SysManage/Controller/Action
    ASP.NET Core MVC 가 IView Location Expander 를 통 해 보기 검색 경 로 를 확장 하 는 것 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 관련 ASP.NET Core MVC 확장 보기 검색 경로 내용 은 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 찾 아 보 세 요.앞으로 도 많은 지원 바 랍 니 다!

    좋은 웹페이지 즐겨찾기