2017-02-17 2 views
1

나는 큰 규모의 응용 프로그램에 세계화/현지화을 추가하려고합니다. 자신을 제외한 모든 것이 완전합니다. 지금까지의 나의 접근 방식은 모든 뷰가 지역화 된 문자열을 얻기 위해 호출 할 수있는 전역 작업을 추가하는 것입니다. 내가 겪고있는 문제는 그러한 글로벌 작업을 만들 수없는 것입니다.MVC 전역 작업을 구현하는 방법

지금까지 action을 내 일반 controller에 추가했는데 모든 작업이 가능합니다. 나는 BaseController에이 방법을 이동할 때, 작업이 더 이상 공격하지 않고 나는 오류 얻을 :

No route in the route table matches the supplied values.

그래서, BaseController 방법은 나를 위해 작동하지 않는.

다음으로 모든보기에서 호출 할 수있는 "영역"외부에 새로운 글로벌 컨트롤러를 만들려고했습니다. 이것도 작동하지 않습니다 ... 나는 행동을 칠 수 없습니다. 모든 내가 파란색으로 만든 controller에 액세스 할 수 있어야합니다 지역의 숫자가 볼 수 있듯이 Controllers 폴더를 강조

enter image description here

:

는 다음과 같은 구조가 어떻게 표시되는지를 보여줍니다. 여기

뷰에서 내 전화입니다 : 여기

@Html.Action("GetLocalizedString", new { key = "Avatar" }) 

그리고 나의 작업입니다 :

[GET("getlocalizedstring")] 
public ActionResult GetLocalizedString(string key) 
{ 
    return Content(ResourceController.GetResourceManger(Identity)[key]); 
} 

다시 말하지만,이 뷰를 렌더링 controller에 작동하지만 내가 호출 할 수 없습니다 지역 외의 컨트롤러 또는 basecontroller에서 가져온 것입니다. 나는 심지어 영역 속성을 없애기 위해 area = string.empty을 추가하려고 시도했지만 여전히 운이 없다.

EDIT 1 (경로 구성 추가) :

private void RegisterMVCRoutes() 
{ 
    try 
    { 
     Application.Lock(); 

     RouteTable.Routes.Clear(); 

     System.Web.Http.GlobalConfiguration.Configure(c => { c.EnableCors(); c.MapHttpAttributeRoutes(); }); 

     RouteTable.Routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
     RouteTable.Routes.IgnoreRoute("{*allaxd}", new { allaxd = @".*\.axd(/.*)?" }); 

     // bring back MiniProfilers 
     if (MiniProfiler.Settings.ProfilerProvider != null) 
      StackExchange.Profiling.UI.MiniProfilerHandler.RegisterRoutes(); 

     RouteTable.Routes.MapAttributeRoutes(config => 
     { 
      config.AddRoutesFromAssembly(System.Reflection.Assembly.GetExecutingAssembly()); 
      config.UseLowercaseRoutes = true; 
     }); 

     AreaRegistration.RegisterAllAreas(); 

     RegisterGlobalFilters(GlobalFilters.Filters); 

     int aspNetRoutingTableEntries = Components.PageRouteEngine.GetInstance().Initialize(RouteTable.Routes); 

     // Page Routes for Design Mode 
     if (Environment == Constants.SystemEnvironment.Staging || Environment == Constants.SystemEnvironment.RD) 
     { 
      RouteTable.Routes.Add(new CMSPageRoute("{*designUrlForAddColumn}", new { area = "CMS", controller = "Designer", action = "AddColumn" }, new { designUrlForAddColumn = @".*\.design/AddColumn(/.*)?" }, "CMS")); 
      RouteTable.Routes.Add(new CMSPageRoute("{*designUrlForAddRowAbove}", new { area = "CMS", controller = "Designer", action = "AddRowAbove" }, new { designUrlForAddRowAbove = @".*\.design/AddRowAbove(/.*)?" }, "CMS")); 
      RouteTable.Routes.Add(new CMSPageRoute("{*designUrlForAddRowAtBottom}", new { area = "CMS", controller = "Designer", action = "AddRowAtBottom" }, new { designUrlForAddRowAtBottom = @".*\.design/AddRowAtBottom(/.*)?" }, "CMS")); 
      RouteTable.Routes.Add(new CMSPageRoute("{*designUrlForDeleteRow}", new { area = "CMS", controller = "Designer", action = "DeleteRow" }, new { designUrlForDeleteRow = @".*\.design/DeleteRow(/.*)?" }, "CMS")); 
      RouteTable.Routes.Add(new CMSPageRoute("{*designUrlForUpdateUseFullWidthForRow}", new { area = "CMS", controller = "Designer", action = "UpdateUseFullWidthForRow" }, new { designUrlForUpdateUseFullWidthForRow = @".*\.design/UpdateUseFullWidthForRow(/.*)?" }, "CMS")); 
      RouteTable.Routes.Add(new CMSPageRoute("{*designUrlForSaveMovedWidget}", new { area = "CMS", controller = "Designer", action = "SaveMovedWidget" }, new { designUrlForSaveMovedWidget = @".*\.design/SaveMovedWidget(/.*)?" }, "CMS")); 
      RouteTable.Routes.Add(new CMSPageRoute("{*designUrlForAddNewWidget}", new { area = "CMS", controller = "Designer", action = "AddNewWidget" }, new { designUrlForAddNewWidget = @".*\.design/AddNewWidget(/.*)?" }, "CMS")); 
      RouteTable.Routes.Add(new CMSPageRoute("{*designUrlForDeleteWidget}", new { area = "CMS", controller = "Designer", action = "DeleteWidget" }, new { designUrlForDeleteWidget = @".*\.design/DeleteWidget(/.*)?" }, "CMS")); 
      RouteTable.Routes.Add(new CMSPageRoute("{*designUrlForMergeColumn}", new { area = "CMS", controller = "Designer", action = "MergeColumn" }, new { designUrlForMergeColumn = @".*\.design/MergeColumn(/.*)?" }, "CMS")); 
      RouteTable.Routes.Add(new CMSPageRoute("{*designUrl}", new { area = "CMS", controller = "Designer", action = "Index" }, new { designUrl = @".*\.design(/.*)?" }, "CMS")); 
     } 

     // Page Routes... Page Index will also be a 404 catch all for routes not found. 
     RouteTable.Routes.Add(new CMSPageRoute("{*url}", new { area = "CMS", controller = "Page", action = "Index" }, "CMS")); 
    } 
    finally 
    { 
     Application.UnLock(); 
    } 
} 

나는 우리의 경로 구성의 대부분의 생각은 우리에게 특정 응용 프로그램입니다,하지만 어쩌면 도움이 될 것입니다. 다시 한번 감사드립니다.

+0

경로 구성은 어떻게 생겼습니까? – jao

+0

사이드 노트 : 정규 자동 생성 리소스 문자열 액세스 클래스가 작동하지 않는 이유는 무엇입니까? –

+0

@ jao - 우리의 라우트 구성은 꽤 큽니다. 나는 순간적으로 업데이트하고 게시 할 것이다. – BrianLegg

답변

0

나는 NightOw888에 의해 제안 된 것과 유사한 접근법을 사용하여 끝냈다. @ Html.Action()을 통해 컨트롤러를 호출하는 대신 확장 메서드를 작성하고 @ Html.Translate()를 통해 호출합니다. 이것은 "번역"코드를 하나의 중앙 위치로 가져오고 어떤 컨트롤러도 수정하지 않아도됩니다. 사용자의 웹 ID에 사용자 언어 기본값을 저장했기 때문에 기본 컨트롤러를 사용하는 대신 HtmlHelper 객체에서이 값을 가져와야했습니다. 내가 사용을 종료 한 코드는 다음과 같습니다. 다른 사람에게 도움이되기를 바랍니다.

public static class ExtensionMethods 
    { 
     public static IHtmlString Translate(this HtmlHelper helper, string text) 
     { 
      if (string.IsNullOrWhiteSpace(text)) 
       return new HtmlString(string.Empty); 

      var identity = (WebIdentity)helper.ViewContext.HttpContext.User.Identity; 

      return new HtmlString(ResourceController.GetResourceManger(identity.Learner.SystemLanguageId)[text]); 
     } 
    } 
관련 문제