正则表达式,用于检查段值是否包含“.HTML”扩展名。对于ASP.NET路由

时间:2012-04-25 02:19:43

标签: c# asp.net regex asp.net-mvc asp.net-mvc-routing

产品的传入网址为mydomain.com/someproductname.html 我需要约束一条路线,这样它才会处理这样的网址。 使检查案例也不敏感。如果我不强制执行此类限制 比以下路线还提取网址请求,如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" });

    }

2 个答案:

答案 0 :(得分:1)

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

使用正则表达式

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

没有正则表达式

bool flag = System.IO.Path.GetExtension(path) == ".html";

答案 1 :(得分:1)

将路线设为

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

应该将它限制在你的.html假文件中。