Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Optional segments can only be placed at the end of your route or better <strong>after required ones</strong>. That's why you have to add an additional route definition without <code>id</code> segment that also defines its default as <code>0</code>.</p> <h2><code>id</code> is numeric</h2> <pre><code>public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" }); routes.MapRoute( "Default", "{id}/{controller}/{action}", new { controller = "Home", action = "Index" }, new { id = @"\d+" } ); routes.MapRoute( "DefaultId", "{controller}/{action}", new { controller = "Home", action = "Index", id = "0" } ); } </code></pre> <p>As you can see, the first route definition also includes a constraint for the <code>id</code> segment which makes a requirement for it to be numeric. That's the trick.</p> <h2><code>id</code> is alphanumeric but not matching any controller name</h2> <p>In case <code>id</code> is not numeric but can be alpha numeric then you will have to take a different approach...</p> <pre><code>public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" }); routes.MapRoute( "DefaultId", "{controller}/{action}", new { controller = "Home", action = "Index", id = "0" }, new { controller = @"Home|Admin|Categories|Items" } ); routes.MapRoute( "Default", "{id}/{controller}/{action}", new { controller = "Home", action = "Index", id = "0" } ); } </code></pre> <p>Then you will have to put a constraint on the <code>controller</code> segment, because you have a finite number of them. Mind that route definitions have swapped so the id-less one is now the first and the one with the <code>id</code> is now second. This should work as long as <code>id</code> can never have the same value as controller name.</p> <h2><code>id</code> can be anything really</h2> <p>But what if <code>id</code> can have a value equal to a controller name? Well in that case you will have to define your routing rules a bit more strictly because you will most likely have to omit default values and provide more routes based on number of segments: zero, one, two, three or even more if required.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload