2017-02-17 9 views
1

グローバリゼーション/ローカライゼーションをかなり大規模なアプリケーションに追加しようとしています。ビュー自体を除いてすべてが完全です。これまでの私のアプローチは、すべてのビューがローカライズされた文字列を取得するために呼び出すことができるグローバルなアクションを追加することです。私が抱えている問題は、このようなグローバルな行動を生み出すことができないということです。MVCグローバルアクションの実装方法

これまでのところ、actionを通常のcontrollerに追加しました。それを打つとすべてが動作します。私はアクションがもうヒットし、私はエラーを取得していないBaseControllerには、このメソッドを移動すると、しかし、:

No route in the route table matches the supplied values.

ので、BaseController方法は私のために働いていません。

次に、どのビューでも呼び出せる「エリア」の外に新しいグローバルコントローラを作成しようとしました。これはどちらも動作しません...私はアクションを打つことはできません。あなたはすべて私がControllersフォルダを強調青色で作成したcontrollerへのアクセス権を持っている必要がありますエリアの数がある見ることができるように

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

NightOwl888で提案されている方法と同様の方法で終了しました。 @ Html.Action()を介してコントローラを呼び出す代わりに、拡張メソッドを記述し、@ Html.Translate()を介して呼び出します。これにより、「翻訳」コードが1つの中央に配置され、コントローラを変更する必要はありません。私たちはユーザーの言語のデフォルトをユーザーのWeb 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]); 
     } 
    } 
関連する問題