用于检查段值是否包含“.HTML”扩展名的正则表达式.用于 ASP.NET 路由
本文关键字:用于 正则表达式 ASP 路由 NET 扩展名 HTML 段值 检查 是否 包含 | 更新日期: 2023-09-27 18:32:13
产品的传入网址将被 mydomain.com/someproductname.html我需要约束一个路由,以便它只处理这样的 url。使校验大小写也不敏感。如果我不执行此类限制比以下路线还拾取 URL 请求,如 mydomain.com/level1category/等。这应该由不同的路线处理。
我想要约束的路由(需要约束"项目名称"段):
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" });
}
将路由设置为
routes.MapRoute(
"ProductLink4", // Route name
"{RootPointer}/{L1Cat}/{L2Cat}/{ItemName}.html",
new
{
controller = "Store",
action = "ViewProduct",
},
应该将其限制在您的.html假文件中。
string path = "mydomain.com/someproductname.html".ToLower();
使用正则表达式
bool flag = Regex.IsMatch(path, @"^.*'.(html)$");
没有正则表达式
bool flag = System.IO.Path.GetExtension(path) == ".html";