2

Generally we have following sample code in our global.asax file. So, my question is how we can have multiple MapRoute and how to use them ???

I want URL like:


http://domain/Home.aspx/Index/Cricket-Ball/12

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default",
                "{controller}/{action}/{id}",
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

I want something like this, but i don't understand how to use this routing so that i can get SEO Friendly URL:


        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default1",
                "{controller}/{action}/{productname}/{id}",
                new { controller = "Home", action = "Index", productname = UrlParameter.Optional, id = UrlParameter.Optional }
            );

            routes.MapRoute(
                "Default",
                "{controller}/{action}/{id}",
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

Thanks in advance.

Sham
  • 691
  • 2
  • 13
  • 26
  • Please provide a sample of what url you would want to create – Ivo Oct 03 '12 at 05:44
  • Check this http://stackoverflow.com/questions/734249/asp-net-mvc-url-routing-with-multiple-route-values. – Mohan Oct 03 '12 at 05:49
  • 1
    @ivowiblo URL will be like : http://domain/Home.aspx/Index/Cricket-Ball/12 – Sham Oct 03 '12 at 05:54
  • And which controller and action you will like to route it? – Ivo Oct 03 '12 at 06:13
  • Also, why you want to add the .aspx? you know you could just do something like domain/home/cricket-ball/12 – Ivo Oct 03 '12 at 06:15
  • @ivowiblo: that's right actually .aspx is not require and that is fine. In the above url Home is Controller Name and Index is Action Name – Sham Oct 03 '12 at 06:38

2 Answers2

3

Since that's not a generic url but a concrete one (pointing to a product), you could use:

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Products",
            "home/index/{productname}/{id}",
            new { controller = "Home", action = "Index" }
        );

        routes.MapRoute(
            "Default",
            "{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

So, everything not matching to "Products" route will go to "Default". Note that I didn't add the ".aspx" to the rout since I believe it's a mistake. If you actually want it, just add it to the route:

routes.MapRoute(
            "Products",
            "home/index.aspx/{productname}/{id}",
            new { controller = "Home", action = "Index" }
        );

Also, I would suggest to use a better url with:

routes.MapRoute(
            "Products",
            "products/{productname}/{id}",
            new { controller = "Home", action = "Index" }
        );
Rodolfo
  • 4,155
  • 23
  • 38
Ivo
  • 8,172
  • 5
  • 27
  • 42
1

The routing example you use is for ASP.NET MVC, not WebForms. You need to use a different variation as described in this blog post:

http://weblogs.asp.net/scottgu/archive/2009/10/13/url-routing-with-asp-net-4-web-forms-vs-2010-and-net-4-0-series.aspx

Example:

routes.MapPageRoute(
    "route-name",
    "products/{id}",
    "~/Products.aspx");

Then in your Page_Load you would need to extract the the route value for id as such:

int id = Page.RouteData.Values["id"] as int;
Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291