簡體   English   中英

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");
}

您可以將約束傳遞給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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM