【发布时间】:2016-09-25 13:57:17
【问题描述】:
我对 MVC 中的某些路由有一个奇怪的问题。这是我的场景:
- 用户在访问该网站后会显示在首页:
testproject.com:57416/Home。 - 用户在左侧有一个菜单,菜单项为“解决方案”。
- 用户按下菜单项“解决方案”,菜单项展开,显示 5 个子项。
- 用户按下子项“Demo”,重定向到
testproject.com:57416/Solutions/SetLayout?layoutString=page1%7Csize15 - 定义参数
layoutString后,网格 (DevExpress) 知道如何过滤要显示的数据。 - 网格呈现给用户,他现在可以在网格中导航,操作布局。
- 用户现在想再次看到干净的演示布局,并且(s)他按下菜单项“解决方案”-> 子项“演示”。
- 在这种情况下,动作
ActionResult SetLayout(string layoutString)没有被命中,而是动作ActionResult Index()被命中,因此没有将布局设置为layoutString没有作为参数发送 - 用户被重定向到的 url,如下所示:
testproejct.com:57416/Solutions?undefined=undefined
这可能读起来有点累,但这是我解释我的场景的最佳方式,因为我以前在 MVC 中从未见过这样的事情。我真的不知道发生了什么,以及为什么动作ActionResult SetLayout(string layoutString) 没有被第二次击中。
我想我应该展示一些代码,以便我们缩小可能性。
_Layout.cshtml(有菜单项)
<li class="has-submenu">
<a href ="#" class="navigation-submenu-toggler show-solution-views">Solutions</a>
<ul class="nav sub-menu-list">
<li>
@Html.ActionLink("Demos", "SetLayout", "Solutions", new { layoutString = "page1|size15" }, null)
</li>
</ul>
</li>
ActionResult SetLayout(string layoutString)
public ActionResult SetLayout(string layoutString)
{
Session["Layout"] = layoutString;
return RedirectToAction("Index", "Solutions");
}
ActionResult Index()
public ActionResult Index ()
{
using(var db = new DataBasecontext())
{
var list = db.Solutions.ToList();
return View(list);
}
}
编辑
我发现只有当我在同一个控制器中时才会发生这种情况。持有动作ActionResult SetLayout(string layoutString) 的控制器是SolutionsController。如果我来自,假设AccountController 一切正常,但从SolutionsController 更改为SolutionsController 中的另一个操作不起作用。
想一想,这可能与我的地图路线有关吗?
routes.MapRoute(
name: "ControllerIdActionId",
url: "{controller}/{id}/{action}/{id2}",
default : new { id2 = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
default : new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Solutions",
url: "{controller}/{id}/{action}",
default : new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
【问题讨论】:
-
@SometimesCode 已经有了,忘记放了。已更正
-
查看Why map special routes first before common routes in asp.net mvc 了解发生这种情况的原因以及可能的解决方法列表。在这里可以说您的路由配置错误。
RouteLink是解决该问题的一种解决方法(不是解决方案),因为传入的请求与您想要的路由不匹配,并且您的路由表中有无法访问的执行路径。
标签: c# asp.net-mvc asp.net-mvc-routing