2
votes

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.

2
Please provide a sample of what url you would want to create - Ivo
@ivowiblo URL will be like : domain/Home.aspx/Index/Cricket-Ball/12 - Sham
And which controller and action you will like to route it? - Ivo
Also, why you want to add the .aspx? you know you could just do something like domain/home/cricket-ball/12 - Ivo

2 Answers

3
votes

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" }
        );
1
votes

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;