[英]MVC weird routing behaviour
我对MVC中的某些路由有一个奇怪的问题。 这是我的情况:
testproject.com:57416/Home
: testproject.com:57416/Home
,就会将用户显示在首页上。 testproject.com:57416/Solutions/SetLayout?layoutString=page1%7Csize15
layoutString
,网格( DevExpress )知道如何过滤要显示的数据。 ActionResult SetLayout(string layoutString)
操作,而是命中了ActionResult Index()
操作,因此未设置布局,因为layoutString
没有作为参数发送。 testproejct.com:57416/Solutions?undefined=undefined
: 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(字符串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 }
);
似乎layoutString参数在第二次触发时缺少参数值,因此将空字符串值插入到会话键中(与该会话相关的Javascript函数将空字符串视为未定义)。
public ActionResult SetLayout(String layoutString)
{
// Test if the string is null or empty before filling session key,
// if it is empty one, leave the session key as is
if (!String.IsNullOrEmpty(layoutString)) {
Session["Layout"] = layoutString;
}
return RedirectToAction("Index", "Solutions");
}
也许这不是最好的方法,我只是从SetLayout方法的角度来看,以适当的查询字符串填充Session键。
在您的操作链接中将第5个参数传递为null,如下所示:
@Html.ActionLink("Demos", "SetLayout", "Solutions", new { layoutString = "page1|size15" },null)
我没有测试过,但是可能是问题所在。
我设法自己解决了这个问题。
我不太清楚问题是什么,但是将Html.ActionLink()
更改为Html.RouteLink()
可以定义要使用的MapRoute
。
我通过使用以下MapRoute
解决了它
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
)
然后,我可以像这样使用Html.RouteLink
:
Html.Routelink("Demos", //Display text
"Default", //The MapRoute to use
new { layoutString = "page1|size15|", //The param (layoutString)
controller = "Solutions", //The controller
action = "SetLayout" }) //The action
我没有对操作进行任何更改:
public ActionResult SetLayout(string layoutString)
{
Session["Layout"] = layoutString;
return RedirectToAction("Index", "Solutions");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.