FriendlyURLs-RouteData不返回任何值 [英] FriendlyURLs - RouteData returns no value

查看:34
本文介绍了FriendlyURLs-RouteData不返回任何值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过NuGet将Microsoft.Asp.Net.FriendlyUrls.Core.dll安装到现有的asp.net 4.5 Webforms网站上.

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

我在 RouteConfig 文件中有以下内容

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

在frurltest.aspx中,我尝试从RouteData中获取"id",如下所示:

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

使用以下网址:http://localhost:48484/frurltest/2

我没有得到"id"的值. RouteData.Values.Count = 0.

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

知道我在这里想念什么吗?

Any idea what am I missing here?

注意:除了获取routedata之外,友好的url功能还起作用,即说我导航到/frutltest.aspx,将其更改为/frutlest,并且可以使用$ RouteUrl生成链接.

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.

更新: 经过反复试验,我注意到如果在MapPageRoute之后移动EnableFriendlyUrls,它将起作用.即

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

我看到的所有示例都用作我最初的无效代码.不知道为什么它对他们有用而不对我有用.这是一个类似的问题: RouteData.值保持为空

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

推荐答案

我知道现在回答还为时已晚,但是对我来说,当我将URL的第一个子句更改为与aspx页面不同的内容时,它就起作用了. 只需在"frurltest/{id}"&中"~/frurltest.aspx" frurltest应该不同,例如:routes.MapPageRoute("frUrl", "frurltestNEW/{id}", "~/frurltest.aspx");

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

这篇关于FriendlyURLs-RouteData不返回任何值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆