【问题标题】:ASP.net WebForms Extensionless URLsASP.net WebForms 无扩展 URL
【发布时间】:2012-12-16 11:47:21
【问题描述】:

我了解在 ASP.net 4.0 中,URL 路由已合并到 Web 表单中。我能做这样的事情真是太好了:

void Application_Start(object sender, EventArgs e) 
{
    RegisterRoutes(RouteTable.Routes);
}
void RegisterRoutes(RouteCollection routes)
{
    routes.MapPageRoute("Category", "list/{id}/{name}", "~/category.aspx");
    routes.MapPageRoute("Product", "item/{id}/{name}", "~/product.aspx");
}

但是,我是否有一种内置方法可以将无扩展名的 url 路由到他们的 .aspx 对应项,而无需为每个文件手动添加路由?例如:

account/login => account/login.aspx
contact-us => contact-us.aspx

谢谢


我在路线的末尾添加了以下作为包罗万象的规则,它似乎正在工作。

routes.MapPageRoute("Default", "{*file}", "~/{file}.aspx");

【问题讨论】:

    标签: asp.net url-rewriting routing url-routing


    【解决方案1】:

    你可以像在 MVC 中那样使用占位符

    VB.NET

        routes.MapPageRoute(
            "ThreeLevels",
            "{folder}/{file}/{id}",
            "~/{folder}/{file}.aspx", True,
            New RouteValueDictionary From {
                {"folder", "Home"},
                {"file", "Default"},
                {"id", Nothing}
            })
    

    C#

        routes.MapPageRoute(
            "ThreeLevels",
            "{folder}/{file}/{id}",
            "~/{folder}/{file}.aspx", true,
            new RouteValueDictionary {
                {"folder", "Home"},
                {"file", "Default"},
                {"id", null}
            });
    

    【讨论】:

      猜你喜欢
      • 2013-04-26
      • 2013-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-19
      • 2015-03-17
      • 1970-01-01
      相关资源
      最近更新 更多