Andrew Berry
Andrew Berry

Reputation: 863

URL Routing in ASP.NET 4.0

I have an app that has 1 aspx and it loads various ascx user controls. This page then forms urls such as page.aspx?section=product&prodid=123 to then access a product etc.

I have been tasked to change this so it uses clean urls. So my plan is to get page.aspx/product/NAME and then that will load the product.

I have 3 main routes:

Product ProductList Content

So. In my global.asax I have:

 void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        RegisterRoutes(RouteTable.Routes);
    }

 public static void RegisterRoutes(RouteCollection routeCollection)
    {
        routeCollection.MapPageRoute("RouteForProduct", "Product/{ProductName}", "~/IRShop.aspx", false, new RouteValueDictionary { { "ProductName", String.Empty } });
        routeCollection.MapPageRoute("RouteForProductList", "ProductList/{CatName}", "~/IRShop.aspx", false, new RouteValueDictionary { { "CatName", String.Empty } });
        routeCollection.MapPageRoute("RouteForContentList", "Content/{PageName}", "~/IRShop.aspx", false, new RouteValueDictionary { { "PageName", String.Empty } });
    }

I have a using System.Web.Routing as well.

In my page.aspx. C# code, I have initialy tried to do:

string id = Page.RouteData.Values["ProductName"].ToString();

But when I go to URL page.aspx/Product/TestProduct and step through the code, Page.RouteData is always null. I know I need more to get my page loading, but from what I have read, I would have thought this would have been enough to at least let me see how my routing is working.

My reference was: http://www.dotnetjalps.com/2011/12/easy-url-rewriting-in-aspnet-40-web.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+blogspot%2FDotNetJalps+%28DotNetJalps%29&utm_content=Google+Reader

Upvotes: 1

Views: 352

Answers (1)

Haney
Haney

Reputation: 34762

Try going to URL "/Product/ProductName" instead of "/page.aspx/Product/ProductName"

Upvotes: 1

Related Questions