我创建了这条路线
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都应该起作用,因为当没有给出任何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强制它,但我认为它应该已经自动选择了这个。
您需要为每个组合创建路线。
查看此Phil Haack文章
routes.MapRoute(
name: "Survey",
url: "{controller}/{action}/{surveyid}/{userid}/{hash}",
defaults: new { controller = "Home", action = "Survey" },
constraints: new { surveyid = @"\d+", userid = @"\d+" }
);
routes.MapRoute(
name: "Survey",
url: "{surveyid}/{userid}/{hash}",
defaults: new { controller = "Home", action = "Survey" },
constraints: new { surveyid = @"\d+", userid = @"\d+" }
);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句