0

I have code that runs on application start that prefixes all of my routes with a {lang} parameter.

The intent is to transform any URL like /page to {lang}/page, with {lang} being a language code ("en", "fr"...).

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapMvcAttributeRoutes();

    foreach (var r in routes.OfType<Route>())
        r.Url = "{lang}/" + r.Url;
}

This works fine on ASP.NET MVC 5.0. For example this action shows the About page on both /en/about-us and /fr/about-us:

[Route("about-us", Name = "About")]
public ActionResult About()
{
    return View();
}

(the {lang} parameter is read in OnActionExecuting and sets the UI culture accordingly.)

However, it no longer works on ASP.NET MVC 5.2.3.

On this version, the page remains accessible on /abous-us, and /en/about-us and /fr/about-us return a 404. It is like the modified routes are not taken into account.

I tried to dig into MapMvcAttributeRoutes to troubleshoot but it looks like the code, and in particular the AttributeRoutingMapper class, was completely rewritten between the 2 versions.

Is there anything I can do to have my modified routes taken into account?

5
  • Did you actually invoke RegisterRoutes? You haven't provided a minimal reproducible example. Commented Oct 16, 2020 at 15:50
  • Sure, here's a full repro: github.com/xps/repro1017 I would expect to be able to navigate to /en/about-us and see "Lang : en" on the page. Commented Oct 17, 2020 at 9:13
  • You need to provide a minimal reproducible example in your question itself. Commented Oct 17, 2020 at 13:17
  • Come on, there is no way I can paste a whole web project (startup code, routing config, controllers, views...) in the question itself. I have included the bits that are relevant. The rest of the code is standard. I have literally changed less than 10 lines from the default template. And yes, the RegisterRoutes method is called from the Application_Start event (as it is by default when you create a new web app). In the repro I posted I have also made sure to make the few modifications to the default web app template in a separate commit. Commented Oct 17, 2020 at 14:38
  • Of course you can provide a minimal reproducible example. Just narrow it down to the minimal amount of code necessary that still reproduces the issue. GitHub is not acceptable, you must provide enough code directly in your question to repro the issue so that the question remains useful to future visitors. Commented Oct 17, 2020 at 19:13

1 Answer 1

1
+500

Without modifying already registered routes, preferred approach would be to use RoutePrefix attribute on controller:

[RoutePrefix("{lang}")]
public class HomeController : Controller
{
    [Route("~/", Name = "Index")] // Use ~ to skip prefix or use optional {lang?} as prefix
    public ActionResult Index()
    {
        return View();
    }

    [Route("about-us", Name = "About")]
    public ActionResult About()
    {
        ViewBag.Message = "Your application description page.";

        return View();
    }

    [Route("contact", Name = "Contact")]
    public ActionResult Contact()
    {
        ViewBag.Message = "Your contact page.";

        return View();
    }
    ...
}

as to why it is not working, unfortunately without looking at the code or comparing routes in both versions you will not have any logical answer.

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

1 Comment

It's not exactly the same as this requires updating each controller but still, that solves my problem, so thank you.

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.