views:

150

answers:

2

Hi folks, i'm trying to make the following routes .. and currently i'm going about this in a really long way.. ie. one route instance for EACH route.

this is what i'm after... (assuming i'm doing a 'stackoverflow website')

/                        <-- root site
/page/{page}             <-- root site, but to the page of questions.
/tag/{tag}/page/{page}   <-- as above, but the questions are filtered by tag
/question/ask            <-- this page :P
/question/{subject}      <-- reading about a question

(and no.. i'm most definitely not doing a stackoverflow website :) )

cheers!

(gawd i find dis all so confusing at times).

A: 

I would change the last url to /question/view/{subject}. Futher Create 3 controllers:

  • PageController
  • TagController
  • QuestionController

in Global.asax create those routes,(take example at the default route)

Hope this helps.

norbertB
Cheers for the comment.I'm not after what controllers i need to make, just how to define the routes properly .. and how many routes.
Pure.Krome
+1  A: 

For your third one, I'd do something like this:

routes.MapRoute("page-tag", "tag/{tag}/page/{page}", new {controller="question", action="FilterByTag"});

Your action method then could look like this:

public class QuestionController : Controller {
  public ActionResult FilterByTag(string tag, int page) {
    //...
  }
}
Haacked
@Phil, if i use your page-tag route ... do i need to make a new route for /page or /tag?alternatively ... should i be having seperate routes for these? or should they be querystrings instead because they are minor value-add actions to the main action (listing questions).
Pure.Krome
It's up to you. Both are valid approaches. Some people don't like query string parameters, but many of the reasons for that view are now defunct. Google does index query string parameters.
Haacked
Phil, if you were going to have to do paging/sorting/filtering on a list of items (eg. 'questions' view, or 'products' view, etc), how would you do it? (post an example route/url to explain your answer). Please :)
Pure.Krome