您将如何在 asp.net mvc 中实现面包屑助手? [英] How would you implement a breadcrumb helper in asp.net mvc?

查看:12
本文介绍了您将如何在 asp.net mvc 中实现面包屑助手?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道你可以很容易地根据数据创建一个助手.因此,如果可能,请仅提交包含获取数据的答案.

I know you could make a helper pretty easily given the data. So, if possible, please only submit answers that also include getting the data.

推荐答案

我们为此使用了一个动作过滤器.

We are using an action filter for this.

...

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var controller = (Controller) filterContext.Controller;
        Breadcrumb[] breadcrumbs = _breadcrumbManager.PushBreadcrumb(_breadcrumbLinkText);
        controller.ViewData.Add(breadcrumbs);
    }

在您提及之前,我也对过滤器属性中的服务位置感到反感 - 但我们几乎没有选择余地.IBreadcrumbManager 看起来像这样:

before you mention it, I too have a distaste for service location in the filter attributes - but we are left with few options. IBreadcrumbManager looks like this:

public interface IBreadcrumbManager
{
    Breadcrumb[] PushBreadcrumb(string linkText);
}

实现将面包屑对象放入会话中.网址是 HttpContext.Current.Request.RawUrl

The implementation puts Breadcrumb objects into the Session. The Url is HttpContext.Current.Request.RawUrl

这篇关于您将如何在 asp.net mvc 中实现面包屑助手?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆