DaveDev
DaveDev

Reputation: 42185

How to fix this route configuration? Configured route returns 404

I'm trying to define a route configuration which will allow an optional 'region' in the following URLs, all of which will default to the home page:

/uk/home // where the 'uk' parameter can be either 'uk' or 'us'
/uk      // where the 'uk' parameter can be either 'uk' or 'us'
/        // in this case, I just want the region to default to 'uk'

The results I'm getting are not ideal though. the first one (/uk/home), and the third one (/) both work, but the second one (/uk), returns 404.

The configurations are defined as:

        routes.MapRoute(
            null,
            "{region}/{controller}",
            new { region = "^UK|US$" },
            new { controller = "Home", action = "Index" }
            );

        routes.MapRoute(
            null,
            "{region}",
            new { region = "^UK|US$" },
            new { controller = "Home", action = "Index" }
            );

        routes.MapRoute(
            null,
            //"{region}",
            "",
            new {region = "UK", controller = "Home", action = "Index" }
            );

What do i need to do to ensure that all 3 urls will default to the home page, with the empty URL defaulting the region to 'uk'?

Upvotes: 0

Views: 113

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038790

Try the following routes:

routes.MapRoute(
    "Region",
    "{region}/{controller}",
    new { controller = "Home", action = "Index" },
    new { region = "^UK|US$" }
);

routes.MapRoute(
    "Default",
    "",
    new { controller = "Home", action = "Index", region = "UK" }
);

Upvotes: 1

Related Questions