locked
Advanced Routing RRS feed

  • Question

  • User1493097378 posted

     Hi people. I need to create a route to catch urls of the form /catalog/4214-notebook-hp-530 where 4214 id de unique id of the product. The problem is that if I use "catalog/{id}-{description}" it doesn't work because of the multiple "-".

     Anybody knows how to map routes like these?

    Thanks

    Wednesday, November 26, 2008 11:56 AM

Answers

  • User1493097378 posted

     Yes, It's a posibility but I wouldn't like to modify my controller action. I solved using /catalog/4214/notebook-hp-530 and a route like this:

                routes.MapRoute(
                    "Catalog",
                    "catalog/{id}/{descripcion}",
                    new { controller = "Product", action = "View", id = 0 },
                    new { id = @"\d+" }
                    );

     Thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 26, 2008 1:29 PM

All replies

  • User-814438144 posted
    What if we declare the id parameter in the controller action as string? I tried with your '4214-notebook-hp-530' and it worked!
    1    public void Catelog(string id)
    2    {
    3       try
    4       {
    5          string[] parts = id.Split('-');
    6          int uniqueId = 0;
    7          Int32.TryParse(parts[0], out uniqueId);
    8          Response.Write("I got the Unique Id: " + uniqueId);
    9       }
    10      catch {
    11         Response.Write("Invalid ID format");
    12      }
    13   }
    
     
    Wednesday, November 26, 2008 1:11 PM
  • User1493097378 posted

     Yes, It's a posibility but I wouldn't like to modify my controller action. I solved using /catalog/4214/notebook-hp-530 and a route like this:

                routes.MapRoute(
                    "Catalog",
                    "catalog/{id}/{descripcion}",
                    new { controller = "Product", action = "View", id = 0 },
                    new { id = @"\d+" }
                    );

     Thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 26, 2008 1:29 PM
  • User-1393251038 posted

    I recently wrote a new route class which supports more complex patterns than the default one. You can read about it, and download the full source code @ my blog http://thecodejunkie.blogspot.com/2008/11/supporting-complex-route-patterns-with.html

    Wednesday, November 26, 2008 3:29 PM