ASP.NET MVC 路由问题
我有关于 MVC 路由的简单问题。我如何构建生成以下链接的 Html.ActionLink http://mysite.com/phones/samsung< br> 现在它生成为 http://mysite.com/phones/brand?brand=samsung
另外我想避免在 URL 中提及操作名称
有我的代码:
路线:
routes.MapRoute(null, "Phones/{brand}",
new { controller = "Phones", action = "Index", brand = UrlParameter.Optional });
控制器:
MySyteDBEntities ms = new MySyteDBEntities();
public ActionResult Index()
{
ViewBag.Brand = ms.Phones.Select(x => x.Brand).Distinct();
return View();
}
public ActionResult Brand(string brand)
{
ViewBag.Standard = ms.Phones.Where(x => x.Brand == brand).Select(x => x.Standard).Distinct();
return View();
}
索引视图代码:
@foreach (string item in ViewBag.Brand)
{
<div>@Html.ActionLink(item, "Brand", new { brand = item })</div>
}
I have simple question about MVC routing. How i can construct Html.ActionLink thhat generates following link http://mysite.com/phones/samsung
Now it's generates as http://mysite.com/phones/brand?brand=samsung
Also i want to avoid mentioning action name in URL
There is my code:
Route:
routes.MapRoute(null, "Phones/{brand}",
new { controller = "Phones", action = "Index", brand = UrlParameter.Optional });
Controller:
MySyteDBEntities ms = new MySyteDBEntities();
public ActionResult Index()
{
ViewBag.Brand = ms.Phones.Select(x => x.Brand).Distinct();
return View();
}
public ActionResult Brand(string brand)
{
ViewBag.Standard = ms.Phones.Where(x => x.Brand == brand).Select(x => x.Standard).Distinct();
return View();
}
Index View code:
@foreach (string item in ViewBag.Brand)
{
<div>@Html.ActionLink(item, "Brand", new { brand = item })</div>
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在您的 MapRoute 中,您没有空间用于操作,因此 ASP.NET 将始终使用默认操作“索引”。
默认情况下,您的路由将如下所示:
您缺少操作部分。
操作链接中的路由值与路由中的参数不匹配,将是查询字符串参数。因此,您需要将路线中的“类别”更改为“品牌”。
试试这个:
。
如果当前视图是通过另一个路由映射的,请务必在 ActionLink 中显式调用控制器,否则它无法识别
brand
参数In your MapRoute you have no space for an action, so asp.net will always use the default action "Index".
By default your routing would look like this:
You're missing the action part.
Routevalues in you actionlink which don't match parameters in your route, will be querystring parameters. So you need to change "category" to " brand" in your route.
Try this:
and
Be sure to call the controller explicit in your ActionLink, if the current view is mapped through another route, otherwise it doesn't recognize the
brand
parameter.尝试(如果有的话,应该在默认路由之前注册此路由)
有了这个, http://mysite.com/电话/ -->应该转到索引操作并且
http://mysite.com/phones/samsung -->;应该去品牌行动。
Try (this route should be registered before the default route, if you have one)
With this, http://mysite.com/phones/ --> should go to Index Action and
http://mysite.com/phones/samsung --> should go to the Brand Action.
我找到了问题的根源。我只需要删除 MapRoute 中的最后一块(品牌可选参数)。野兔是代码:
i'm found source of problem. I just need to remove last piece (brand optional parameter) in MapRoute. Hare is code:
通过使用 id 作为参数名称,将阻止路由使用 querystring 键值对。
您的无参数 GET 和 View 代码应该仍然可以工作,无需任何更改。
By using id as the parameter name, it will prevent the routing from using the querystring key value pairs.
Your parameterless GET and View code should still work without any changes.
如果我没记错的话, {brand} 部分需要作为参数的一部分包含在内:
只需记住,它需要位于任何默认路由之前。
If I remember correctly, the {brand} part needs to be included as is as part of your parameters:
Just remember, it needs to go before any default routes.