如何在 MVC 母版页中设置导航变量

发布于 2024-07-26 22:15:48 字数 1147 浏览 1 评论 0原文

我需要一些导航选项,这些选项需要特定于当前用户的键,这些键驻留在母版页中。 我需要一些关于最佳实践的建议。

在母版页的左侧导航中包含以下链接

http://www.example.com/manageShop/123

http://www.example.com/addProductToShop/123

http://www.example.com/addStaffToShop/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

http://www.example.com/addProductToShop/123

http://www.example.com/addStaffToShop/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡紫姑娘! 2024-08-02 22:15:48

您可以为此创建自定义过滤器:

public class UserKeyAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        filterContext.Controller.ViewData["UserKey"] = UserKey;
    }
}

并将其用于您的控制器或控制器操作

[UserKey]
public class YourController : Controller
{

// or

public class YourController : Controller
{
    [UserKey]
    public ActionResult Index()
    {

或使用 Controller.OnActionExecuting() (甚至如 Arnis L. 所说为此创建基本控制器):

public class BaseController : Controller
{
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        filterContext.Controller.ViewData["UserKey"] = UserKey;
    }
}

// and then derive your controllers from BaseController

public class YourController : BaseController
{

You can create custom filter for this:

public class UserKeyAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        filterContext.Controller.ViewData["UserKey"] = UserKey;
    }
}

and use it on your controller or controller actions

[UserKey]
public class YourController : Controller
{

// or

public class YourController : Controller
{
    [UserKey]
    public ActionResult Index()
    {

or use Controller.OnActionExecuting() (or even create base controller for this as Arnis L. said):

public class BaseController : Controller
{
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        filterContext.Controller.ViewData["UserKey"] = UserKey;
    }
}

// and then derive your controllers from BaseController

public class YourController : BaseController
{
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文