4

I installed the Microsoft.Asp.Net.FriendlyUrls.Core.dll via NuGet to an existing asp.net 4.5 webforms website.

I have the following in the RouteConfig file

 public static void RegisterRoutes(RouteCollection routes)
        {
            var settings = new FriendlyUrlSettings();
            settings.AutoRedirectMode = RedirectMode.Permanent;
            routes.EnableFriendlyUrls(settings);

            routes.MapPageRoute("frUrl", "frurltest/{id}", "~/frurltest.aspx");
        }
**Global.asax**
void Application_Start(object sender, EventArgs e) 
{

        RouteConfig.RegisterRoutes(System.Web.Routing.RouteTable.Routes);
}

In the frurltest.aspx I try to get the "id" from the RouteData like below:

string id = String.Empty;
if (RouteData.Values["id"] != null)
{
   id = RouteData.Values["id"].ToString();
}

Response.Write("id= " + id);

With the following url: http://localhost:48484/frurltest/2

I don't get the value for the "id". RouteData.Values.Count=0.

Any idea what am I missing here?

Note: Other than getting the routedata, the friendly url functionality is working i.e. say I navigate to /frutltest.aspx, it is changed to /frutlest and I can generate the links using $RouteUrl.

Update: After trial and errors I noticed if I move the EnableFriendlyUrls after MapPageRoute, it works. i.e.

public static void RegisterRoutes(RouteCollection routes)
{
      routes.MapPageRoute("frUrl", "frurltest/{id}", "~/frurltest.aspx");

      var settings = new FriendlyUrlSettings();
      settings.AutoRedirectMode = RedirectMode.Permanent;
      routes.EnableFriendlyUrls(settings);
}

All the examples I have see, uses as my initial non-working code. No idea why it works for them and not for me. Here's one similar question: RouteData.Values stays Empty

Community
  • 1
  • 1
gbs
  • 7,196
  • 5
  • 43
  • 69
  • `Response.Write`, not seen this since *Classic* days! – Christian Phillips Jul 30 '14 at 20:35
  • We don't generally remove the tags from the Title when they're made a natural part of the question phrase... We remove tags from the Title them when they're simply tacked onto the beginning or end. – Erik Philips Jul 30 '14 at 20:40
  • @ErikPhilips so for my understanding.. is "FriendlyURLs in asp.net ....." allowed? – gbs Jul 30 '14 at 20:44
  • 1
    You can typically use tags in the title when they are naturally part of the question. For example `Create a relationship in Entity Framework for my models?`. What is normally not accepted are questions where the tags are just thrown onto the begging or end, where removing them does not change the question of the sentence. For example `Entity Framework - Create a relationship for my models`. [It may be subtle but it's preferred](http://meta.stackoverflow.com/a/253076/209259). – Erik Philips Jul 30 '14 at 20:48
  • Had same problem...It's really weird.Thanks for solution :) – Abolfazl Hosnoddin May 11 '15 at 22:46

1 Answers1

0

I know it's too late to answer, but for me it worked when I changed the first clause of the URL to something different from the aspx page. simply in "frurltest/{id}" & "~/frurltest.aspx" the frurltest should be different for example: routes.MapPageRoute("frUrl", "frurltestNEW/{id}", "~/frurltest.aspx");

Yasin
  • 51
  • 11
  • I discovered this today as well and I can't believe that the routing engine breaks if the first part of the route is the same as a physical page. Very annoying. Glad you figured it out. – clockwiseq Dec 01 '16 at 17:23
  • @Yasin Even though this shouldn't be happening (but unfortunately it does!) the correct answer is that the EnableFriendlyUrls should be moved after the MapPageRoute line(s). – dpant Oct 23 '17 at 22:25
  • or use routes.RouteExistingFiles = true; – KitAndKat Apr 26 '22 at 21:20