c# - Set a route to an area without specifying the area name in the url in MVC -


i have mvc 4 application. in application have areas, url not resolved properly. locationarearegistration.cs follows:

context.maproute(             "location_default",             "{culture}/{controller}/{action}/{id}",             new { culture = "en", action = "locationindex", id = urlparameter.optional },             new { controller = "(locationindex)" }         ); 

my route.config follows:

routes.maproute(                  name: "default",                  url: "{culture}/{controller}/{action}/{id}",                  defaults: new { culture = "en", controller = "home", action = "index", id = urlparameter.optional },                  namespaces: new[] { "locator.areas.location.controllers" }                ).datatokens.add("area", "location"); 

i have tried change route.config below:

routes.maproute(              name: "default",              url: "{culture}/{controller}/{action}/{id}",              defaults: new { culture = "en", controller = "home", action = "index", id = urlparameter.optional },              namespaces: new[] { "locator.areas.location.controllers" }            ); 

none of approaches worked , the resource cannot found error.

however when change locationarearegistration.cs follows, works:

context.maproute(             "location_default",             "{culture}/location/{controller}/{action}/{id}",             new { culture = "en", action = "locationindex", id = urlparameter.optional },             new { controller = "(locationindex)" }         ); 

but not want url contain location(area name). doing wrong?

edit

the urls going like:

http://localhost/en/locationindex/locationindex 

here en current culture, home controller name , index action method name.

to make location area default set of routes mvc application, need define routeconfig.cs follows:

public class routeconfig {     public static void registerroutes(routecollection routes)     {         routes.ignoreroute("{resource}.axd/{*pathinfo}");          routes.maproute(             name: "default_localized",             url: "{culture}/{controller}/{action}/{id}",             defaults: new { controller = "home", action = "index", id = urlparameter.optional },             constraints: new { culture = new cultureconstraint(defaultculture: "en", pattern: "[a-z]{2}") },             namespaces: new string[] { "locator.areas.location.controllers" }         ).datatokens["area"] = "location";          routes.maproute(             name: "default",             url: "{controller}/{action}/{id}",             defaults: new { culture = "en", controller = "home", action = "index", id = urlparameter.optional },             namespaces: new string[] { "locator.areas.location.controllers" }         ).datatokens["area"] = "location";     } } 

note replace functionality of default controllers in application , send of requests location namespace.

you should not put route definitions locationarearegistration.cs file. ensure run last , don't screw of other area routes.

here definition of cultureconstraint. see this answer more details how localize routes.

using system.text.regularexpressions; using system.web; using system.web.routing;  public class cultureconstraint : irouteconstraint {     private readonly string defaultculture;     private readonly string pattern;      public cultureconstraint(string defaultculture, string pattern)     {         this.defaultculture = defaultculture;         this.pattern = pattern;     }      public bool match(         httpcontextbase httpcontext,          route route,          string parametername,          routevaluedictionary values,          routedirection routedirection)     {         if (routedirection == routedirection.urlgeneration &&              this.defaultculture.equals(values[parametername]))         {             return false;         }         else         {             return regex.ismatch((string)values[parametername], "^" + pattern + "$");         }     } }