ASP.NET URL 路由

ASP.NET URL Routing

如何让我的 URL 像:www.mydomain.com/JohnSmith

而不是:www.mydomain.com/Customers/JohnSmith

这是我的 global.asax 文件

void Application_Start(object sender, EventArgs e)
{
    RegisterRoutes(RouteTable.Routes);
}

static void RegisterRoutes(RouteCollection routes)
{
    routes.MapPageRoute("Customers", "Customers", "~/Customers.aspx");
    routes.MapPageRoute("CustomerDetails", "Customers/{CustomerName}", "~/CustomerDetails.aspx");
}

我一直在涉足 ASP.NET,我 运行 进入这些页面...

https://msdn.microsoft.com/en-us/library/dd329551.aspx

https://msdn.microsoft.com/en-us/library/cc668201.aspx

您可以将约束传递给 MapPageRoute,特别是,这是签名:

public Route MapPageRoute(
    string routeName,
    string routeUrl,
    string physicalFile,
    bool checkPhysicalUrlAccess,
    RouteValueDictionary defaults,
    RouteValueDictionary constraints,
    RouteValueDictionary dataTokens
)

这是您需要的:

static void RegisterRoutes(RouteCollection routes)
{
    //rest of routes...
    routes.MapPageRoute(
        "CustomerDetails", 
        "{CustomerName}", 
        "~/CustomerDetails.aspx", 
        true, 
        null, 
        new RouteValueDictionary { {"CustomerName", @"\w+" } },
        null);
}