我创建了这条路线
routes.MapRoute(
name: "Survey",
url: "{controller}/{action}/{surveyid}/{userid}/{hash}",
defaults: new { controller = "Home", action = "Survey" },
constraints: new { surveyid = @"\d+", userid = @"\d+" }
);
当我然后浏览到
http://localhost:3086/Home/Survey/1/1/3r2ytg
它可以工作,但是如果我浏览到
http://localhost:3086/1/1/3r2ytg
这没用。
如果我像这样改变路线
routes.MapRoute(
name: "Survey",
url: "{surveyid}/{userid}/{hash}",
defaults: new { controller = "Home", action = "Survey" },
constraints: new { surveyid = @"\d+", userid = @"\d+" }
);
正好相反会起作用(这是有道理的)。
但我对第一条路线感到好奇,我认为这两个 URL 都应该起作用,因为它应该在没有给出默认控制器和动作时抓取默认控制器和操作。
更新
最后我只用了这个
routes.MapRoute(
name: "Survey",
url: "{surveyId}/{userId}/{hash}",
defaults: new { controller = "Home", action = "Survey" },
constraints: new { surveyId = @"\d+", userId = @"\d+" }
);
因为那是我想要的行为。但是,当我然后打电话时
@Url.Action("Survey", "Home", new
{
userId = @Model.UserId,
surveyId = survey.Id,
hash = HashHelpers.CreateShortenedUrlSafeHash(@Model.SecretString + survey.Id.ToString() + @Model.UserId.ToString())
})
它生成
/Admin/Home/Survey?userId=25&surveyId=19&hash=2Norc
而不是一条闪亮的路径。我可以用 Url.RouteUrl 强制它,但我认为它应该自动选择这个。