4

我正在使用 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... 这很难理解。

任何提示或帮助???

谢谢

4

1 回答 1

3

您需要使用正确的重载:

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

GetRouteUrl重载使用RouteName

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

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

于 2012-11-23T01:50:42.380 回答