scgough
scgough

Reputation: 5252

MVC Routes - Urls based on fixed list of parameters

I have a large group of URLs I need to 301 redirect. I've set up a number of routes as 'catches' for the various 301s but I somehow seem to have created a redirect loop.

Can someone confirm - would the following be a valid url and enforce the parameter to ONLY be from the pipe delimited list (i.e. if the values aren't matched, the route won't catch)

routes.MapRoute("MyRouteName", // Route name
    "first-folder/second-folder/{param1}", // URL with parameters
    new
    {
        controller = "Redirect",
        action = "MyRedirectHandler",
        newRouteName = "mynewroute",
        folder1= "foo",
        folder2 = "bar",
        set = "option1|option2|option3"
    });

so, in this case:

mysite.com/first-folder/second-folder/option1 => mysite.com/mynewroute/foo/bar

mysite.com/first-folder/second-folder/option2 => mysite.com/mynewroute/foo/bar

mysite.com/first-folder/second-folder/option88 => 404

the params in set are used only to catch the url, my MyRedirectHandler is a controller action that will do the 301 to the route i specify in newRouteName.

Upvotes: 0

Views: 78

Answers (1)

scgough
scgough

Reputation: 5252

OK, I posted too soon. For anyone experiencing a similar issue this loop was down to the difference between Route param defaults and param constraints. The example above should be:

routes.MapRoute("MyRouteName", // Route name
  "first-folder/second-folder/{param1}", // URL with parameters
new
{
    controller = "Redirect",
    action = "MyRedirectHandler",
    newRouteName = "mynewroute",
    folder1= "foo",
    folder2 = "bar" 
},  //param defaults
new {
    set = "option1|option2|option3"
} //param constraints
);

Upvotes: 1

Related Questions