假设我们有一个带有路由数据的页面(例如,/employees/1)和一个不带路由数据的页面(例如,/home/about)。我们可以使用ASP.NET MVC路由系统来处理这两种情况。下面是一些代码示例:
我们可以在RouteConfig.cs中定义以下路由规则:
routes.MapRoute( name: "Employees", url: "employees/{id}", defaults: new { controller = "Employees", action = "Index" } );
然后在EmployeesController.cs中,我们可以使用以下代码来获取路由数据:
public ActionResult Index(int id) { // Code to retrieve data for employee with id = id // and pass it to the view return View(employeeData); }
我们可以在RouteConfig.cs中定义以下路由规则:
routes.MapRoute( name: "About", url: "home/about", defaults: new { controller = "Home", action = "About" } );
然后在HomeController.cs中,我们可以创建以下操作来呈现视图:
public ActionResult About() { return View(); }
这是一种简单的方法来路由到具有或不具有路由数据的页面。当然,我们也可以使用其他路由规则来更精细地控制路由过程。