I'm working on adding globalization/localization to a fairly large scale application. Everything is complete except for the views themselves. My approach so far is to add a global action that every view can call to get localized strings. The problem I am having is that I can't seem to create such a global action.
So far, I added an action
to my regular controller
... and I can hit it and everything works. However, when I move this method into the BaseController the action is not hit anymore and I get the error:
No route in the route table matches the supplied values.
So, BaseController
method isn't working for me.
Next, I tried creating a new global controller outside of the "Areas" that any view should be able to call. This doesn't work either... I can't hit the action.
Here's what the structure looks like:
As you can see there are a number of areas which all must have access to the controller
I created in the blue highlighted Controllers
folder.
Here is my call from the view:
@Html.Action("GetLocalizedString", new { key = "Avatar" })
And here is my action:
[GET("getlocalizedstring")]
public ActionResult GetLocalizedString(string key)
{
return Content(ResourceController.GetResourceManger(Identity)[key]);
}
Again, this works on the controller
which rendered the view, but I can't call it from a controller outside of the area or the basecontroller
. I even tried adding area = string.empty
to get rid of the area property, but still no luck.
EDIT 1 (Adding Route Config):
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();
}
}
I think most of our route configuration is application specific only to us, but maybe it will help. Thanks again.