在MVC中是否可以使用单个控制器"ListController"执行以下操作来处理以下页面...
www.example.com/List/Cars/ForSale/{id}可选
www.example.com/List/Cars/ForRent/{id}可选
www.example.com/List/Search/
www.example.com/List/Boats/ForSale/{id}可选
www.example.com/List/Boats/ForRent/{id}可选
www.example.com/List/Boats/Search/
如果没有,除了将CarsController和BoatsController分开之外,还有什么方法可以绕过它吗?他们将使用相同的逻辑,只是希望URL不同.
Mat*_*ela 17
你绝对可以做到这一点.使用路由很简单.您可以将不同的URL路由到控制器中的不同操作.
以下是定义上述网址的示例:
routes.MapRoute("CarSale"
"/List/Cars/ForSale/{id}",
new { controller = "list", action = "carsale", id = UrlParameter.Optional } );
routes.MapRoute("ListSearch"
"/List/search",
new { controller = "list", action = "search"} );
routes.MapRoute("BoatSale"
"/List/Boats/ForSale/{id}",
new { controller = "list", action = "boatsale", id = UrlParameter.Optional } );
Run Code Online (Sandbox Code Playgroud)
然后在您的控制器中,您将拥有每个的操作方法:
public ListController
{
// ... other stuff
public ActionResult CarSale(int? id)
{
// do stuff
return View("CarView");
}
public ActionResult BoatSale(int? id)
{
// do stuff
return View("BoatView");
}
// ... other stuff
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
35093 次 |
| 最近记录: |