3

I've been reading a lot about the SEO friendly URL features in ASP.Net. Most of what I've read involves taking a URL that uses query-string params and making it pretty. I'm interested in making standard URL's pretty. For example:

http://mysite.com/aboutus.aspx

should be...

http://mysite.com/about-us

I've found that the code below satisfies the requirement:

void Application_Start(object sender, EventArgs e) 
{
    // Enable routing
    RegisterRoutes(RouteTable.Routes);
}

void RegisterRoutes(RouteCollection routes)
{        
    // About us section routes
    routes.MapPageRoute(
        "AboutUsRoute",
        "{about-us}",
        "~/aboutus.aspx"
     );
}

My issue is that I'll have to manually specify a route for each page in the site. Is there a better way to do this?

James Hill
  • 60,353
  • 20
  • 145
  • 161
  • 1
    I would say just use MVC... doesn't address the question, I know, but it _is_ a solution :) – Andrew Grothe Oct 04 '12 at 13:28
  • 2
    @agrothe, It's an **awful** solution. I don't want to start a flame war, but I prefer web forms. Why? It's what I'm comfortable with at the moment. – James Hill Oct 04 '12 at 13:29
  • 2
    Looks like you need dynamic routing, see here: http://stackoverflow.com/questions/890275/webforms-custom-dynamic-routing – Andrew Grothe Oct 04 '12 at 13:30
  • ASP.NET MVC uses the same routing as web forms, wouldn't some of features that let you avoid routing boilerplate in MVC help here? – millimoose Oct 04 '12 at 13:30
  • @James_Hill no flame wars indeed! MVC does make SEO URLs easier, but it's still possible with Webforms. Use the pageprocessor idea in the prior link, or, in the past I've used a single query string like domain.com/?Content=News/World/some-content-page. – Andrew Grothe Oct 04 '12 at 13:36
  • If you are using >= .net 4 then I believe this will apply to you: 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 – JDandChips Oct 04 '12 at 13:38
  • @millimoose, yes it does, but you have to map each page as opposed to the dymanic routes that MVC uses. See http://msdn.microsoft.com/en-us/library/cc668177.aspx – Andrew Grothe Oct 04 '12 at 13:38
  • @JDandChips, it does apply, but it's exactly what I'm using. I'm trying to avoid creating a route for each and every page. – James Hill Oct 04 '12 at 13:55

2 Answers2

3

This simple route will map the route Url to the physical .aspx page.

routes.MapPageRoute("Page", "{name}", "~/{name}.aspx");

So /about maps to /about.aspx, /contact-us maps to contact-us.aspx, etc.

James Lawruk
  • 30,112
  • 19
  • 130
  • 137
3

Microsoft recently released a new library to make this kind of thing quick and easy in Web Forms. Here is a link with useful info: http://www.hanselman.com/blog/IntroducingASPNETFriendlyUrlsCleanerURLsEasierRoutingAndMobileViewsForASPNETWebForms.aspx

John
  • 772
  • 7
  • 17