【问题标题】:Wrong Page.GetRouteUrl() in asp.net 4.0 application. Any help for URL routingasp.net 4.0 应用程序中的错误 Page.GetRouteUrl()。 URL路由的任何帮助
【发布时间】:2012-09-07 13:18:11
【问题描述】:

我正在使用 Web 表单创建 asp.net Web 应用程序,我正在使用注册路由到产品友好的 URL。

以下是 Global.asax.cs 中的代码;

void Application_Start(object sender, EventArgs e) { // 在应用程序启动时运行的代码 RegisterRoutes(RouteTable.Routes); }

    void RegisterRoutes(RouteCollection routes)
    {
        // Register a route for Categories/All
        routes.MapPageRoute(
           "All Categories",      // Route name
           "Categories/All",      // Route URL
           "~/AllCategories.aspx" // Web page to handle route
        );

        // Route to handle Categories/{CategoryName}. 
        // The {*CategoryName} instructs the route to match all content after the first slash, which is needed b/c some category names contain a slash, as in the category "Meat/Produce"
        // See http://forums.asp.net/p/1417546/3131024.aspx for more information
        routes.MapPageRoute(
           "View Category",               // Route name
           "Categories/{*CategoryName}",  // Route URL
           "~/CategoryProducts.aspx"      // Web page to handle route
        );
        // Register a route for Products/{ProductName}
        routes.MapPageRoute(
           "View All Product",           // Route name
           "Products", // Route URL
           "~/ViewProducts.aspx"      // Web page to handle route
        );


        // Register a route for Products/{ProductName}
        routes.MapPageRoute(
           "View Product",           // Route name
           "Product/{ProductName}", // Route URL
           "~/ViewProduct.aspx"      // Web page to handle route
        );


        // Register a route for Products/{ProductName}
        routes.MapPageRoute(
           "Add Product",           // Route name
           "NewProduct", // Route URL
           "~/AddProduct.aspx"      // Web page to handle route
        );


    }

现在在我放的一页

        lnkNewProduct.NavigateUrl = Page.GetRouteUrl("Add Product");

运行项目时会产生错误的href url。

谁能告诉我为什么会这样?目前它显示的 URL 像 http:\localhost:5770\Categories\All?Length=11... 这很难理解。

任何提示或帮助???

谢谢

【问题讨论】:

    标签: asp.net .net-4.0 routes


    【解决方案1】:

    你需要使用正确的重载:

    Page.GetRouteUrl("Add Products", null);
    

    这个GetRouteUrl 重载使用RouteName。

    您也可以使用 Route Url 表达式:

    http://msdn.microsoft.com/en-us/library/system.web.compilation.routeurlexpressionbuilder.aspx

    【讨论】:

    • 感谢布赖恩的回答。它有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-09
    • 1970-01-01
    • 2013-06-24
    • 1970-01-01
    • 1970-01-01
    • 2011-07-30
    • 1970-01-01
    相关资源
    最近更新 更多