用两个参数在asp.net 4.0中重写url

时间:2014-07-04 14:42:23

标签: asp.net c#-4.0 url-rewriting url-routing

我的动态生成网址很少,比如

http://localhost:35228/begineercontrols?name=untitled&id=2
http://localhost:35228/begineercontrols?name=linkbutton&id=34
http://localhost:35228/begineercontrols?name=lablebutton&id=5 

以及更多,使用路由我删除了.aspx扩展,这很好,现在我想使上面的网址更友好,如

http://localhost:35228/begineercontrols/untitled/2 

如何为所有动态生成的网址实现此功能请帮助我 谢谢你的推荐!

1 个答案:

答案 0 :(得分:0)

在App_Start / RouteConfig.cs中,修改如下代码

 public static void RegisterRoutes(RouteCollection routes)
    {
        var settings = new FriendlyUrlSettings();
        settings.AutoRedirectMode = RedirectMode.Permanent;
        routes.EnableFriendlyUrls(settings);
        // Add Routes.
        RegisterCustomRoutes(RouteTable.Routes);
    }

   private static void RegisterCustomRoutes(RouteCollection routes)
    {
        routes.MapPageRoute(
            "begineercontrolsRoute",
            "begineercontrols/{name}/{id}",
            "~/begineercontrols.aspx"
        );
    }