FriendlyURLs - RouteData returns no value

China☆狼群 提交于 2019-12-07 07:29:10

问题


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


回答1:


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");



来源:https://stackoverflow.com/questions/25046377/friendlyurls-routedata-returns-no-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!