我正在练习 ASP.NET MVC 路由,现在我陷入了一种我不知道如何解决的情况。我有两个控制器和两个控制器中的操作以及
RouteConfig
类中的两条路由 - 看起来像这样:
routes.MapRoute(
name: "Students",
url: "{Class}/{Students}",
defaults: new { controller = "Class", action = "Students" });
routes.MapRoute(
name: "SubjectDetail",
url: "{Class}/{Subject}",
defaults: new { controller = "Subject", action = "SubjectDetail"});
现在的问题是,当我转到
class/Students
URL 时,它工作正常,但如果是 class/subject
,它会再次将我重定向到 class/Students
URL。
我知道存在一些路线模式错误。如何解决这个问题?
谢谢
尝试:
routes.MapRoute(
name: "Students",
url: "classes/{classId}/students/{studentId}",
defaults: new { controller = "Class", action = "Students" });
routes.MapRoute(
name: "SubjectDetail",
url: "classes/{classId}/subjects/{subjectId}",
defaults: new { controller = "Subject", action = "SubjectDetail"});