MVC web site design questions

Last post 08-07-2008 11:46 AM by ravivb.net. 3 replies.

Sort Posts:

  • MVC web site design questions

    08-06-2008, 3:59 PM
    • Member
      1 point Member
    • jamesewelch
    • Member since 02-21-2008, 4:58 PM
    • San Diego
    • Posts 9

    I'm learning ASP.NET MVC (I mostly understand how it works), but I have a few questions about designing/organization your web sites.

    1. When should you create a new controller and when should you just add a new action?

    In the download, there's a HomeController that has Home, Contact, and About actions that interact with 3 different ASPX pages (almost said web forms). It seems like you could have had 3 different controllers here, but that would bring the Controller to ViewPage ratio to a 1:1. 

    When should you just add more to a single controller versus just making a new controller? How much is too much to put into one controller? Should it be divided up based on your Model/Data?

     

    2. Where is a standard place to put UserControls?

    I've created a top level directory ~/UserControls, but should this be under Views? Or Views/Section/UserControls? Is there a standard way for this?

     

    3. Routing.

    I tried messing around with adding a few new routes last night, but I kept getting my ActionLinks pointing to ~/Home (default route, I think). I switch from using routes.Map to routes.Add and it seemed to be working. Is there a way to use RegEx to reduce the number of routes in the global.asax file?

    Right now, I have a bunch of routes for a blog like route:

    /blog/

    /blog/{year}/

    /blog/{year}/{month}/

    /blog/{year}/{month}/{day}/

    /blog/{year}/{month}/{day}/{slug}

    Then I need to add pagination support, so I needed to add:

    /blog/page/{page}

    /blog/{year}/page/{page}

    /blog/{year}/{month}/page/{page}

    /blog/{year}/{month}/{day}/page/{page}

    Is there a better way for this?

    Thanks,

  • Re: MVC web site design questions

    08-06-2008, 5:47 PM
    • Member
      26 point Member
    • chrisntr
    • Member since 01-23-2008, 12:40 AM
    • Manchester, UK
    • Posts 14

    For point 1; I generally have a Controller for every main section on the website. So Home, Contact, About would be three separate controllers. Then you'd have different actions to handle the pages within the section. I think the important thing is to keep separation of concerns as main priority.

    It's also worth creating a BaseController class which extends the Controller class to put in common functionality across all different sections/controllers.

    If any one has any better ideas then I'd also be interested in hearing them.

  • Re: MVC web site design questions

    08-07-2008, 3:02 AM
    • Participant
      938 point Participant
    • CW2
    • Member since 05-28-2007, 5:38 PM
    • Czech Republic
    • Posts 207

    chrisntr:
    For point 1; I generally have a Controller for every main section on the website. So Home, Contact, About would be three separate controllers.

    I have also a controller for every website section, but I think in terms of functionality, not just individual pages - e.g. Home (resp. Index), Contact and About are actions of Home/SiteController. For the main content section there is some kind of Catalog/Products/Blog/WhateverController, with usual actions like Index, Details. Then for example SearchController for search functionality*, Account/UserController etc. Most of actions are handled by 'default' routes, in case I need a little bit different URL scheme I can easily create a specific route (e.g. I don't really like "/home/..." URLs, so I have mapped the HomeController actions to the root).

     * Originally, it was simple Search action of Home/SiteController, but then I moved it to a new controller, to handle site search, specialized catalog/product search and filters, sitemap for search engines etc.

    Additionally, I use partial classes to separate more complex actions of the same controller, in order to keep the source 'clean'.

  • Re: MVC web site design questions

    08-07-2008, 11:46 AM
    • Participant
      882 point Participant
    • ravivb.net
    • Member since 01-30-2007, 2:17 AM
    • Posts 187

     I don't think there are any hard rules to what should be its own controller...In general, i would say, logical groups of your websites should correspond respective controllers.

    For one of the sites i am working on i have controllers like

    Home - for some specific tasks on Home page

    Search - Search Related

    Product - Product Related

    User - Subscriptions, other stuff

    Account - Sign In , Sign Off, reset password etc

    It made sense to me at that point and i would say don't make it big deal to have an extra controller...Just concentrate on how it lets you test you code and the maintainability that comes with it.

     2. I put my user controls in "View/Shared/UserControls"

    3. I couldn't figure out (at least its not built in yet) the regex routing. I believe we would have to develop our own routing rules engine or hope that it will be part of the next versions...

    good luck 

    Filed under:
Page 1 of 1 (4 items)