我有一个Global.asax,我在其中定义路由(参见下面的RegisterRoutes方法)
我将有很多路由,所以我想将此方法保存在一个单独的静态类中,使用Global Asax中的命名空间导入并使用Application_Start中的方法。
不幸的是我无法做到。
所以我的问题:
如果是,我该怎么办?
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
);
// Register a route for Products/{ProductName}
routes.MapPageRoute(
"View Content", // Route name
"Content/{ContentId}", // Route URL
"~/Cms/FrontEndCms/Content.aspx" // Web page to handle route
);
}
protected void Application_Start(object sender, EventArgs e)
{
// ROUTING.
RegisterRoutes(RouteTable.Routes);
}
答案 0 :(得分:3)
你绝对应该能够做到这一点:
protected void Application_Start(object sender, EventArgs e)
{
// ROUTING.
Helper.RegisterRoutes(RouteTable.Routes);
}
public static class Helper
{
public static 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
);
// Register a route for Products/{ProductName}
routes.MapPageRoute(
"View Content", // Route name
"Content/{ContentId}", // Route URL
"~/Cms/FrontEndCms/Content.aspx" // Web page to handle route
);
}