localization - ASP.NET MVC Routing. Resource not found -
i'm trying implement localization in asp.net mvc application using routing.
for example: www.example.com/home/index - show content default culture. www.example.com/en/home/index - show english content. www.example.com/ru/home/index - russian , on...
so, i've created localization attribute each action:
public class localizationattribute : actionfilterattribute { public override void onactionexecuting(actionexecutingcontext filtercontext) { if (filtercontext.routedata.values["lang"] != null && !string.isnullorwhitespace(filtercontext.routedata.values["lang"].tostring())) { var lang = filtercontext.routedata.values["lang"].tostring(); thread.currentthread.currentculture = cultureinfo.createspecificculture(lang); thread.currentthread.currentuiculture = cultureinfo.createspecificculture(lang); } else { var langheader = string.empty; langheader = filtercontext.httpcontext.request.userlanguages[0]; thread.currentthread.currentculture = cultureinfo.createspecificculture(langheader); thread.currentthread.currentuiculture = cultureinfo.createspecificculture(langheader); } base.onactionexecuting(filtercontext); } } i'm adding attribute every action in controllers.
and registering routes:
public static void registerroutes(routecollection routes) { routes.ignoreroute("{resource}.axd/{*pathinfo}"); routes.maproute( "localization", "{lang}/{controller}/{action}/{id}", new { lang = "en-us", controller = "home", action = "index", id = urlparameter.optional } ); routes.maproute( "default", "{controller}/{action}/{id}", new { controller = "home", action = "index", id = urlparameter.optional } ); } navigation www.example.com/about/contacts works fine. when navigate www.example.com/en/about/contacts or that, 404 error: resource not found. so, problem?
thanks in advance help!
by navigating www.example.com/en/about/contacts still invoking default route. in case both routes match last specified 1 (top bottom) invoked.
so when navigate url default route assume following: controller: en action: id: contacts
you should add constraints routes make them more specific. or move "localization" route below "default" route, way both still match in case last 1 "localization".
if have troubleshoot similar in future suggest use routedebugger nuget package, understand routes match request , 1 execute , parameters.. here's link phil haack blog entry it: http://haacked.com/archive/2011/04/12/routedebugger-2.aspx
Comments
Post a Comment