如何在 MVC 母版页中设置导航变量
我需要一些导航选项,这些选项需要特定于当前用户的键,这些键驻留在母版页中。 我需要一些关于最佳实践的建议。
在母版页的左侧导航中包含以下链接
http://www.example.com/manageShop/123
其中“123”是当前用户管理的商店 ID。 我需要某种方式将其传递到母版页
目前我正在采取一些措施:
<li><%= Html.ActionLink<ShopController>(x => x.ManageShop((int)Session["ShopKey"]), "Manage")%></li>
我认为这是一个好主意,因为我只需在会话中设置 ShopKey 一次即可完成,缺点是 iv 注意到如果您打开站点,则会话会混合为两个选项卡。
或者我尝试了这个:
<li><%= Html.ActionLink<ShopController>(x => x.ManageShop((int)ViewData["ShopKey"]), "Manage")%></li>
但这意味着您必须在每个控制器的每个操作中继续设置 ViewData。 这太糟糕了。
编辑:我已经看过下面建议的 eu-ge-ne 之类的过滤器,但这并没有真正解决我的问题,因为我仍然存在到处设置 ShopKey 的问题?
解决办法是什么?
I need to have some navigation options, that require keys that are specific to the current user, that reside in a masterpage. I need some advice on best practise.
In have the following links in a left nav in a masterpage
http://www.example.com/manageShop/123
Where '123' is the shop id that the current user is the manager of. I need some way of passing this to the masterpage
Currently I'm going something to this effect:
<li><%= Html.ActionLink<ShopController>(x => x.ManageShop((int)Session["ShopKey"]), "Manage")%></li>
I thought this was a good idea as I only have to set the ShopKey once in the session and its done, the down side is that iv noticed that the session gets mixed if you have the site open is two tabs.
Alternatively I tried this:
<li><%= Html.ActionLink<ShopController>(x => x.ManageShop((int)ViewData["ShopKey"]), "Manage")%></li>
But this means you have to keep setting the ViewData in every action in every controller. Which is awful.
EDIT: I have had alook at filters like eu-ge-ne suggested below, but I dont this really solves my problem as I still have the issue of setting the ShopKey everywhere?
What is the solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以为此创建自定义过滤器:
并将其用于您的控制器或控制器操作
或使用 Controller.OnActionExecuting() (甚至如 Arnis L. 所说为此创建基本控制器):
You can create custom filter for this:
and use it on your controller or controller actions
or use Controller.OnActionExecuting() (or even create base controller for this as Arnis L. said):