简体   繁体   中英

ASP.NET MVC : Not sure how to make these routes

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).

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) {
    //...
  }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM