【发布时间】:2015-08-14 08:19:39
【问题描述】:
我的网址
http://www.domain.com/Products/{别名}-{ID}
还有我的路线
routes.MapRoute(
name: "ProductDetail",
url: "Products/{Alias}-{detailId}",
defaults: new { controller = "Products", action = "ProductDetail", id = UrlParameter.Optional }
);
在控制器中
public ActionResult ProductDetail(int? detailId)
{
var pro = db.Products.Find(detailId);
if (pro == null)
{
return RedirectToAction("Index", "NotFound");
}
return View(pro);
}
现在,我想在我的 URL 中隐藏 ID,例如
我该怎么做
【问题讨论】:
-
隐藏是什么意思?如果不将其添加到 url,则不会在控制器中获取它。
-
如果
Alias对某个产品来说是唯一的,那么您可以完全删除 id 并使用它,但前提是您确定它是唯一的。如果您解释为什么要隐藏它可能会有所帮助,因为可能还有其他方法可以实现您想要实现的目标 -
@StephenMuecke 如果我不在 URL 中添加 detailID,那么如何在控制器中获取 detailID?
-
@musefan,OP 需要在方法中添加一个参数(它在路由中完全没用)
-
您不能(除非您的发布表单数据)。
id = UrlParameter.Optional也有点毫无意义——你没有一个名为id的参数。而且你也没有{alias}的参数
标签: asp.net asp.net-mvc-4