1

I have three routes that look like that:

all.html

all/page-{numpage}.html

all/page-{numpage}-limit-{limit}.html.

First two work ok - which means that in my controller I'm getting the value of numpage or 1 if none is given:

public ViewResult All(int numpage = 1, int limit = 10) {}

numpage is whatever I typed in the address bar

Third route doesn't work at all - as if I went to the first route (all.html), so it's value is equal to 1, and limit is 10 - the defaults. However when I go to all/page-4.html?limit=3 I get correct values. What am I doing wrong? :D

One more thing - I create my routes dynamically, so code for registering them looks like that (rcache returns correct list of routes):

List<Tuple<Dictionary<string, string>, string, string, string>> routes = rcache.GetRoutes();

            foreach (var route in routes) {
                foreach (KeyValuePair<string, string> kvp in route.Item1) {
                    context.MapRoute(
                        route.Item4,
                        kvp.Value,
                        new { controller = route.Item2, action = route.Item3, id = UrlParameter.Optional, name = UrlParameter.Optional, numpage = UrlParameter.Optional, limit = UrlParameter.Optional }
                    );
                }
            }
3
  • let's say that these three above are the only ones Commented Jul 11, 2012 at 18:38
  • 2
    ok well u are fighting with yourself. why dont u put the last rule as first. and see what happens? order of route rules makes a difference. Commented Jul 11, 2012 at 18:49
  • Damn... after years of php development I should have remember this :D. Post this comment as an answer so I'll accept it. And thanks! Commented Jul 11, 2012 at 18:53

1 Answer 1

2

ok well you are fighting with yourself. Why don't you put the last rule as first, and see what happens? Order of route rules makes a difference.

Just a quick note. Simplify your design and avoid complexity. What you are doing might work, but it's a terrible choice. Don't do it.

first of all you shouldnt allow anyone to add/edit/delete your routes. if someone doesnt really understand how it works, you are allowing them to break your application. i would definetely avoid it.

Well lets say you have to do it. then you need to validate the routes and rules as well, you cant just push it to your route table.

Also, lets say they add a route and it is not working, because your controller doesnt support it. you will take the blame. let them come to you with a request and then you add it to your route table.

You are the author of this code, no one else will be able to maintain what you wrote or what you were thinking while you were writing.

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

1 Comment

One quick question - so how can I do it simpler? Right now I have an xml file with defined routes, then I parse it and pass data to MapRoute method of AreaRegistrationContext class. With this code everybody in my team will be able to understand it - if they want another route - they'll add it to routes.xml, and eventually modify some controller.

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.