I have a site with controllers and two other areas with the respective controllers for each. One of the controllers within the area has a language contraint code say en. By default it works perfectly fine. But when I try to use the Route
specification in the controllers it is building the routes in misleading way.
The RouteConfig.cs file looks like below
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.LowercaseUrls = true;
AreaRegistration.RegisterAllAreas();
routes.MapRoute(
name: "DefaultWithLanguageAndOrg",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MyProj.Website.WebApp.Controllers" }
);
}
Part of Area registration file looks like below:-
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Test_default",
"{lang}/Test/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { lang = new LanguageRouteConstraint() },
new[] { "MyProj.Website.WebApp.Areas.Test.Controllers" }
);
}
And controller looks like below:-
[RouteArea("Test")]
[RoutePrefix("certificate")]
public class CertificationsController : Controller
{
[Route("Home")]
public ActionResult Home()
{
return View();
}
}
My expecation is to have the URL structure like site/en/Test/certificate/Home
but I'm not able to add the prefix en
before RouteArea.
Note:-
- Tried adding en into the RouteArea like
[RouteArea("en/Test")]
it executes the action but expects the views folder to be moved inside en. That is not a proper solution, other routes without the Route specification will not work. - Tried adding Area and language contraint within the RoutePrefix like
[RoutePrefix("{lang}/Test/certificate/Home")]
, it executes the action but not renders the view. It searches the view in the path like~/Views/Certifications/Home.cshtml
where Area Test is missing, it should be like~/Test/Views/Certifications/Home.cshtml
. And this format as well[RoutePrefix("en/{area}/certificate")]
no luck.