Question

I am successfully using the 'Friendly URL' module in ASP.NET 4.5

In route config I can add a route like this:

routes.MapPageRoute("mypage", "mypage/{mypageName}", "~/mypage.aspx");

for a URL like this:

mysite.com/mypage/hello

in the page 'mypage.aspx' I can get URL segments like this:

using Microsoft.AspNet.FriendlyUrls;

// Get URL segments
IList<string> segments = Request.GetFriendlyUrlSegments();
if (segments.Count > 0)
{
    // Get first segment
    string url = segments[0];



}

However, I cannot get this working for root URL's. e.g. 'my site.com/ttee'

I want to get 'ttee' and pass it into a page. But 'Request.GetFriendlyUrlSegments()' returns 0 for the root.

How best can I do this?

Was it helpful?

Solution

routes.MapPageRoute("mypage", "mypage/{mypageName}", "~/mypage.aspx");

This will work only for URLs in this format:

www.example.com/mypage/changingparthere

If you want to make it

www.example.com/changablemypage

Set it to:

routes.MapPageRoute("mypage", "{mypageName}", "~/mypage.aspx");

But as you can see, it will catch literally everything. So make sure it is the last routing on Global.asax.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top