ASP.net WebForms无扩展URL

时间:2013-01-02 03:14:02

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

据我所知,在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");
}

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

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

由于


我已经在路线的末尾添加了以下内容作为一个包罗万象的规则,它似乎正在发挥作用。

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

1 个答案:

答案 0 :(得分:5)

您可以使用像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}
        });