在Global.asax.cs中注册路由:
routes.MapRoute(
name: "ProductDetails",
url: "product/details/{id}",
defaults: new { controller = "Product", action = "Details", id = UrlParameter.Optional }
);
在控制器中,可以在操作方法中接受名称为"id"的参数:
public ActionResult Details(int id)
{
// 根据ID获取产品详细信息并返回视图
}
在视图中可以使用UrlHelper来在链接中生成包含路由数据的URL:
@Url.Action("Details", "Product", new { id = 1234 })
routes.MapRoute(
name: "ProductDetails",
url: "product/details",
defaults: new { controller = "Product", action = "Details" }
);
在控制器中,可以从请求中获取查询字符串或表单数据:
public ActionResult Details()
{
int id = Int32.Parse(Request.QueryString["id"]);
// 根据ID获取产品详细信息并返回视图
}
在视图中可以使用类似以下的链接来将参数传递到控制器:
Product Details