1

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:-

  1. 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.
  2. 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.
1

1 Answer 1

1
+50

You can override the View() of the Controller

like -

protected override ViewResult View(string ViewName, string masterName, object model)
    {
        return PrepareView(ViewName, masterName, model);
    }

private ViewResult PrepareView(string ViewName, string masterName, object model)
    {
        renderview = base.View("~/Views/Shared/" + ViewName + ".cshtml", masterName, model); 
        return base.View(ViewName, masterName, model);
    }

This is just a example code, similar to this you can override the path using your languagecode.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.