我想重写URL
http://localhost:56713/Home/UserDetails?Code=223322
使用
标签
http://localhost:56713/223322
我已经在 StartUp.cs 中写了以下内容,但它没有起作用。
var rewrite = new RewriteOptions()
.AddRewrite(@"{$1}", "Home/UserDetails?Code={$1}",true);
我想重写URL
http://localhost:56713/Home/UserDetails?Code=223322
标签
http://localhost:56713/223322
var rewrite = new RewriteOptions()
.AddRewrite(@"{$1}", "Home/UserDetails?Code={$1}",true);
var rewrite = new RewriteOptions().AddRewrite(
@"^Home/UserDetails?Code=(.*)", // RegEx to match Route
"Home/{$1}", // URL to rewrite route
skipRemainingRules: true // Should skip other rules
);
@"{$1}"
是行不通的。术语$1
表示使用RegEx解析的值。您尚未执行任何RegEx,因此实际上您告诉它“每当URL为null
时重写我的URL”。显然,这不太可能。@"^Home/UserDetails?Code=(\d+)"
(\d+)
"表示RegEx将匹配“一个或多个数字”并将其存储为变量。由于这是括号中唯一包含的变量,因此该值存储在$1
中。"Home/$1"
AddRewrite
方法:AddRewrite(
@"^Home/UserDetails?Code=(\d+)", // RegEx to match URL
"Home/$1", // URL to rewrite
true // Stop processing any aditional rules
);