Page view counter

Extra defaults bug

Last post 12-25-2007 12:50 AM by robconery. 6 replies.

Sort Posts:

  • Extra defaults bug

    12-21-2007, 5:05 PM
    • Loading...
    • tgmdbm
    • Joined on 12-17-2007, 2:08 PM
    • Posts 815
    • ASPInsiders

    Hi,

    Steps to reproduce:

    • Create a new MVC App
    • Create an MVC Controller called Products.
    • Add 2 controller actions, one called List which takes no parameters, one called View which takes an int Id
    • Create 2 views for the 2 actions.
    • Open Views/Shared/Site.Master
    • Add 2 links next to Home and About
      • <%= Html.ActionLink<ProductController>(x => x.List(), "List Products") %>
      • <%= Html.ActionLink<ProductController>(x => x.View(1), "View Product 1") %>
    • Test to make sure the app works (click on the 4 links in the header)
    • Open Global.asax.cs
    • There should be 2 Routes, add an extra Default to the first Route but not called controller, action, or id, e.g. page. (See below)
    • Test the app again. Pay close attention to the URLs. The app is now broken.

    The first route in Global.asax.cs should look like this...

          RouteTable.Routes.Add( new Route
          {
            Url = "[controller]/[action]/[id]",
            Defaults = new { action = "Index", id = (string)null, page = 1 },
            RouteHandler = typeof( MvcRouteHandler )
          } );

    Simply adding page = 1 (or anything which isn't matched in the Url) breaks all the links which match this route.

    Filed under: , ,
  • Re: Extra defaults bug

    12-23-2007, 11:22 PM
    • Loading...
    • tgmdbm
    • Joined on 12-17-2007, 2:08 PM
    • Posts 815
    • ASPInsiders

    could someone confirm this as a bug?

    If the instructions aren't clear let me know and I'll try to clarify.

  • Re: Extra defaults bug

    12-24-2007, 2:11 PM
    • Loading...
    • robconery
    • Joined on 02-23-2005, 10:16 PM
    • Posts 183
    • AspNetTeam

     I think there's some confusion on the way routing works here - lemme see if I can help.

    If you want to create a way to handle paging for a list, you can do that by creating a new route rule:

    [controller]/list/[page]

    and make sure this is placed on top of the other rules since the first valid rule for any URL will be applied (hope this makes sense) - in other words there is an order of precedence here.

    OK, that said, if you try to pass in a value of "page" as an argument, there has to be an argument named "page" on the action so we can set that argument when we "method.Invoke()". This invocation is explicit - not done by ordering.

    Does this help? 

     

  • Re: Extra defaults bug

    12-24-2007, 5:47 PM
    • Loading...
    • tgmdbm
    • Joined on 12-17-2007, 2:08 PM
    • Posts 815
    • ASPInsiders

    Sorry, Not really.

    It's all well and good that i can add [page] to the url. but I WHAT my urls to look like /Products/List?page=x but i also want the url /Products/List to go to page 1 by default.

    Obviously i could check RouteData and if "page" isn't found set it to 1, but this means i can't parameterise my methods with page, and i'll have to do that for each controller which does paging.

  • Re: Extra defaults bug

    12-24-2007, 6:46 PM
    • Loading...
    • robconery
    • Joined on 02-23-2005, 10:16 PM
    • Posts 183
    • AspNetTeam

    To be sure I know what the issue is, let me repeat it back.

    You're defining in your route some defaults, with id being null, the action being "Index", and page=1. With your route, you're specifying:

          RouteTable.Routes.Add( new Route
          {
            Url = "[controller]/[action]/[id]",
            Defaults = new { action = "Index", id = (string)null, page = 1 },
            RouteHandler = typeof( MvcRouteHandler )
          } );

    And the issue you're seeing is that if an action has an argument called "page", it's not being set to 1 - correct? 

    If I'm getting closer here - did you define a route the ties "page" into the Url? Like this:

          RouteTable.Routes.Add( new Route
          {
            Url = "[controller]/[action]/[page]",
            Defaults = new { action = "List", page = 1 },
            RouteHandler = typeof( MvcRouteHandler )
          } ); 

     You have to be explicit with respect to args, their name, and the Routes. Am I getting closer? :):)

     

  • Re: Extra defaults bug

    12-24-2007, 10:14 PM
    • Loading...
    • tgmdbm
    • Joined on 12-17-2007, 2:08 PM
    • Posts 815
    • ASPInsiders

    see topic http://forums.asp.net/t/1197244.aspx for an example of this bug.

    ---

    Oh, and merry christmas. 

  • Re: Extra defaults bug

    12-25-2007, 12:50 AM
    • Loading...
    • robconery
    • Joined on 02-23-2005, 10:16 PM
    • Posts 183
    • AspNetTeam

     You too :)

Page 1 of 1 (7 items)
Microsoft Communities