Extra defaults bughttp://forums.asp.net/t/1197234.aspx/1?Extra+defaults+bugTue, 25 Dec 2007 04:50:52 -050011972342073352http://forums.asp.net/p/1197234/2073352.aspx/1?Extra+defaults+bugExtra defaults bug <p>Hi,</p> <p>Steps to reproduce:</p> <ul> <li>Create a new MVC App</li><li>Create an MVC Controller called Products.</li><li>Add 2 controller actions, one called List which takes no parameters, one called View which takes an int Id</li><li>Create 2 views for the 2 actions.<br> </li><li>Open Views/Shared/Site.Master</li><li>Add 2 links next to Home and About <ul> <li>&lt;%= Html.ActionLink&lt;ProductController&gt;(x =&gt; x.List(), &quot;List Products&quot;) %&gt;</li><li>&lt;%= Html.ActionLink&lt;ProductController&gt;(x =&gt; x.View(1), &quot;View Product 1&quot;) %&gt;</li></ul> </li><li>Test to make sure the app works (click on the 4 links in the header)<br> </li><li>Open Global.asax.cs <br> </li><li>There should be 2 Routes, add an extra Default to the first Route but not called <i>controller</i>, <i>action</i>, or <i>id</i>, e.g. <i>page</i>. (See below)<br> </li><li>Test the app again. Pay close attention to the URLs. The app is now broken.<br> </li></ul> <p>The first route in Global.asax.cs should look like this...</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RouteTable.Routes.Add( new Route<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Url = &quot;[controller]/[action]/[id]&quot;,<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Defaults = new { action = &quot;Index&quot;, id = (string)null, page = 1 },<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RouteHandler = typeof( MvcRouteHandler )<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } );</p> <p>Simply adding page = 1 (or anything which isn't matched in the Url) breaks all the links which match this route.<br> </p> 2007-12-21T21:05:46-05:002074923http://forums.asp.net/p/1197234/2074923.aspx/1?Re+Extra+defaults+bugRe: Extra defaults bug <p>could someone confirm this as a bug?</p> <p>If the instructions aren't clear let me know and I'll try to clarify.<br> </p> 2007-12-24T03:22:28-05:002075768http://forums.asp.net/p/1197234/2075768.aspx/1?Re+Extra+defaults+bugRe: Extra defaults bug <p>&nbsp;I think there's some confusion on the way routing works here - lemme see if I can help.</p> <p>If you want to create a way to handle paging for a list, you can do that by creating a new route rule:</p> <p>[controller]/list/[page]</p> <p>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.</p> <p>OK, that said, if you try to pass in a value of &quot;page&quot; as an argument, there has to be an argument named &quot;page&quot; on the action so we can set that argument when we &quot;method.Invoke()&quot;. This invocation is explicit - not done by ordering.</p> <p>Does this help?&nbsp;</p> <p>&nbsp;</p> 2007-12-24T18:11:26-05:002075921http://forums.asp.net/p/1197234/2075921.aspx/1?Re+Extra+defaults+bugRe: Extra defaults bug <p>Sorry, Not really.<br> </p> <p>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.</p> <p>Obviously i could check RouteData and if &quot;page&quot; isn't found set it to 1, but this means i can't parameterise my methods with <span style="font-style:italic">page</span>, and i'll have to do that for each controller which does paging.</p> 2007-12-24T21:47:24-05:002075944http://forums.asp.net/p/1197234/2075944.aspx/1?Re+Extra+defaults+bugRe: Extra defaults bug <p>To be sure I know what the issue is, let me repeat it back.</p> <p>You're defining in your route some defaults, with id being null, the action being &quot;Index&quot;, and page=1. With your route, you're specifying:</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RouteTable.Routes.Add( new Route<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Url = &quot;[controller]/[action]/[id]&quot;,<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Defaults = new { action = &quot;Index&quot;, id = (string)null, page = 1 },<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RouteHandler = typeof( MvcRouteHandler )<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } ); <br> </p> <p>And the issue you're seeing is that if an action has an argument called &quot;page&quot;, it's not being set to 1 - correct?&nbsp;</p> <p>If I'm getting closer here - did you define a route the ties &quot;page&quot; into the Url? Like this:</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RouteTable.Routes.Add( new Route<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Url = &quot;[controller]/[action]/[page]&quot;,<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Defaults = new { action = &quot;List&quot;, page = 1 },<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RouteHandler = typeof( MvcRouteHandler )<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } );&nbsp; </p> <p>&nbsp;You have to be explicit with respect to args, their name, and the Routes. Am I getting closer? :):)<br> </p> <p>&nbsp;</p> 2007-12-24T22:46:02-05:002076023http://forums.asp.net/p/1197234/2076023.aspx/1?Re+Extra+defaults+bugRe: Extra defaults bug <p>see topic <a href="/t/1197244.aspx">http://forums.asp.net/t/1197244.aspx</a> for an example of this bug.</p> <p>---</p> <p>Oh, and merry christmas.&nbsp;</p> 2007-12-25T02:14:59-05:002076115http://forums.asp.net/p/1197234/2076115.aspx/1?Re+Extra+defaults+bugRe: Extra defaults bug <p>&nbsp;You too :)<br> </p> 2007-12-25T04:50:52-05:00