0

产品的传入 url 将是 mydomain.com/someproductname.html 我需要限制一条路线,以便它只处理这样的 url。也使检查大小写不敏感。如果我不强制执行这样的限制,那么下面的路由也会获取 URL 请求,例如 mydomain.com/level1category/ 等,这些请求应该由不同的路由处理。

我要限制的路线(需要限制“ItemName”段):

     routes.MapRoute(
     "ProductLink4", // Route name
     "{RootPointer}/{L1Cat}/{L2Cat}/{ItemName}", // URL with parameters
     new
     {
         controller = "Store",
         action = "ViewProduct",
     },
     new { controller = "Store", action = "ViewProduct" ItemName = @"[^\\s]+(\\.(?i)(html))" });

                routes.MapRoute(
    "ProductLink3", // Route name
    "{RootPointer}/{L1Cat}/{ItemName}", // URL with parameters
    new
    {
        controller = "Store",
        action = "ViewProduct",

    },
    new { controller = "Store", action = "ViewProduct" });

                routes.MapRoute(
    "ProductLink2", // Route name
    "{RootPointer}/{ItemName}", // URL with parameters
    new
    {
        controller = "Store",
        action = "ViewProduct",

    },
    new { controller = "Store", action = "ViewProduct" });

    }
4

2 回答 2

1
string path = "mydomain.com/someproductname.html".ToLower();

使用正则表达式

bool flag = Regex.IsMatch(path, @"^.*\.(html)$");

没有正则表达式

bool flag = System.IO.Path.GetExtension(path) == ".html";
于 2012-04-25T02:22:55.873 回答
1

将您的路线设置为

 routes.MapRoute(
 "ProductLink4", // Route name
 "{RootPointer}/{L1Cat}/{L2Cat}/{ItemName}.html",
 new
 {
     controller = "Store",
     action = "ViewProduct",
 },

应该将其限制为您的 .html fakefiles。

于 2012-04-25T02:46:06.667 回答