nawaz uddin
nawaz uddin

Reputation: 365

Multi-lingual routes for areas in ASP.NET MVC

I've got an issue with ASP.NET MVC routing for areas. I set up the routing with language without areas, and it is working fine.

Myrouteconfig.cs:

  routes.MapRoute(
      name: "LocalizedDefault",
      url: "{lang}/{controller}/{action}",
      defaults: new { controller = "Home", action = "Index" },
      constraints: new { lang = "ar-ag|en-us" }
  );

In areasregistration class I setup the routes like this:

context.MapRoute(
            "Exhibitor_default",
            "{lang}/AreasName/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
        );

I also created razorviewengineclass method for setup language currentculture.

This is my razor view engine class method for setup the routes

public void SetCurrentCulture(string lang)
{
    _currentCulture = lang;

    ICollection<string> arViewLocationFormats =
            new string[] { "~/Views/{1}/" + lang + "/{0}.cshtml", };          
    ICollection<string> arBaseViewLocationFormats = new string[] {
            @"~/Views/{1}/{0}.cshtml",
            @"~/Views/Shared/{0}.cshtml" 
    };

    this.ViewLocationFormats = arViewLocationFormats.Concat(arBaseViewLocationFormats).ToArray();
}

I try this for areas routes. I have multiple areas with different names and the same controller.

ICollection<string> arViewLocationFormats =
            new string[] { "~/Views/{1}/" + lang + "/{0}.cshtml",
                           "~/"+ lang + "/Areas/Views/{1}/{0}.cshtml"
            };          
ICollection<string> arBaseViewLocationFormats = new string[] {
            @"~/Views/{1}/{0}.cshtml",
            @"~/Views/Shared/{0}.cshtml",
            @"~/"+lang+"/Areas/Views/{1}/{0}.cshtml",
            @"~/"+lang+"/Areas/Views/Shared/{0}.cshtml"
        }

I am getting this error

Multiple types were found that match the controller named 'Dashboard'

Upvotes: 0

Views: 58

Answers (1)

Mauro Raymondi
Mauro Raymondi

Reputation: 1

I solved by adding MapRoute into each RegistrationArea method. Here is a sample.

    public override void RegisterArea(AreaRegistrationContext context) 
    {
        context.MapRoute(
            name: "AreaSites_language",
            url: "{lang}/AreaSites/{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            constraints: new { lang = @"it|fr" }
        );

        context.MapRoute(
            "AreaSites_default",
            "AreaSites/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
        );
    }

Upvotes: 0

Related Questions