一个路由指定多个控制器
Multiple controllers specified in one route
在我们的 ASP.NET MVC 架构中,我们有以下路由结构:
http://localhost:80/Students/Details/2 - will give me details about student with id == 2, details include all the classes current student have.
http://localhost:80/Classes/Details/2 - will give me details about classes with id == 2, details include all the students current class have.
http://localhost:80/Schedule/Details/class=2&student=2 - will give me details about specific schedule for student with id=2 that has class with id=2
到目前为止一切正常。
现在,我们的团队对 asp.net mvc 还很陌生,我们正在考虑让路由更直观一些。这样一来,我们将拥有一条较长的路线,而不是三条单独的路线,如下所示:
http://localhost:80/Students/2/Classes/2/Homework/
它会像这样工作:
http://localhost:80/Students/ - will give list of students
http://localhost:80/Students/2 - will give details about student 2
http://localhost:80/Students/2/Classes - will give all the classes for student with id 2
http://localhost:80/Students/2/Classes/2 - will give schedule for class with id 2 and student with id 2.
不确定是否是 reasonable/possible,只是想征求更多意见
MVC 路由映射到控制器内的单个操作 class。
您可以为每种情况配置一条路线,映射到每种情况的不同操作。请记住,路由应该从更具体到更通用。
在你的 App_Start/RouteConfig.cs:
routes.MapRoute(
name: "StudentSchedule",
url: "students/{studentID}/classes/{classID}",
defaults: new { controller = "Home", action = "StudentSchedule" }
);
routes.MapRoute(
name: "StudentClasses",
url: "students/{studentID}/classes",
defaults: new { controller = "Home", action = "StudentClasses" }
);
routes.MapRoute(
name: "StudentDetails",
url: "students/{studentID}",
defaults: new { controller = "Home", action = "StudentDetails" }
);
routes.MapRoute(
name: "StudentsList",
url: "students",
defaults: new { controller = "Home", action = "Students" }
);
因此,您的操作将如下所示,您必须添加代码以从数据库中获取数据,您可以在应用程序的分离 classes/layers 上共享代码:
public ActionResult Students()
{
var viewmodel = new StudentsViewModel();
...
return View(viewmodel);
}
public ActionResult StudentDetails(int studentID)
{
var viewmodel = new StudentDetailsViewModel();
...
return View(viewmodel);
}
public ActionResult StudentClasses(int studentID)
{
var viewmodel = new StudentClassesViewModel();
...
return View(viewmodel);
}
public ActionResult StudentSchedule(int studentID, int classID)
{
var viewmodel = new StudentScheduleViewModel();
...
return View(viewmodel);
}
在我们的 ASP.NET MVC 架构中,我们有以下路由结构:
http://localhost:80/Students/Details/2 - will give me details about student with id == 2, details include all the classes current student have.
http://localhost:80/Classes/Details/2 - will give me details about classes with id == 2, details include all the students current class have.
http://localhost:80/Schedule/Details/class=2&student=2 - will give me details about specific schedule for student with id=2 that has class with id=2
到目前为止一切正常。
现在,我们的团队对 asp.net mvc 还很陌生,我们正在考虑让路由更直观一些。这样一来,我们将拥有一条较长的路线,而不是三条单独的路线,如下所示:
http://localhost:80/Students/2/Classes/2/Homework/
它会像这样工作:
http://localhost:80/Students/ - will give list of students
http://localhost:80/Students/2 - will give details about student 2
http://localhost:80/Students/2/Classes - will give all the classes for student with id 2
http://localhost:80/Students/2/Classes/2 - will give schedule for class with id 2 and student with id 2.
不确定是否是 reasonable/possible,只是想征求更多意见
MVC 路由映射到控制器内的单个操作 class。
您可以为每种情况配置一条路线,映射到每种情况的不同操作。请记住,路由应该从更具体到更通用。
在你的 App_Start/RouteConfig.cs:
routes.MapRoute(
name: "StudentSchedule",
url: "students/{studentID}/classes/{classID}",
defaults: new { controller = "Home", action = "StudentSchedule" }
);
routes.MapRoute(
name: "StudentClasses",
url: "students/{studentID}/classes",
defaults: new { controller = "Home", action = "StudentClasses" }
);
routes.MapRoute(
name: "StudentDetails",
url: "students/{studentID}",
defaults: new { controller = "Home", action = "StudentDetails" }
);
routes.MapRoute(
name: "StudentsList",
url: "students",
defaults: new { controller = "Home", action = "Students" }
);
因此,您的操作将如下所示,您必须添加代码以从数据库中获取数据,您可以在应用程序的分离 classes/layers 上共享代码:
public ActionResult Students()
{
var viewmodel = new StudentsViewModel();
...
return View(viewmodel);
}
public ActionResult StudentDetails(int studentID)
{
var viewmodel = new StudentDetailsViewModel();
...
return View(viewmodel);
}
public ActionResult StudentClasses(int studentID)
{
var viewmodel = new StudentClassesViewModel();
...
return View(viewmodel);
}
public ActionResult StudentSchedule(int studentID, int classID)
{
var viewmodel = new StudentScheduleViewModel();
...
return View(viewmodel);
}