如何使用 MvcSiteMap 为 ASP.Net MVC2 创建站点地图节点

发布于 2024-09-30 23:07:51 字数 993 浏览 9 评论 0原文

我想使用 MvcSiteMap 定义控制器和操作的站点地图,以便生成面包屑和菜单。

我尝试使用下面的装饰器以编程方式添加节点,但不幸的是它不会使我的树像我想要的那样。

[MvcSiteMapNodeAttribute(Title = "Home"]
[MvcSiteMapNodeAttribute(Title = "Services", ParentKey = "Home")]
[MvcSiteMapNodeAttribute(Title = "Service detail", ParentKey = "Services")]
[MvcSiteMapNodeAttribute(Title = "Edit", ParentKey = "Service detail")]

我如何装饰我的行为以确保孩子/父母关系按照我想要的方式建立?

[HandleError]
public class HomeController : Controller
{
    // Home
    public ActionResult Index ()
    {
        return View();
    }
}

[HandleError]
public class ServiceController : Controller
{
    // Home > Services
    public ActionResult Index ()
    {
        return View();
    }

    // Home > Services > Service detail
    public ActionResult Details (int id)
    {
        return View();
    }

    // Home > Services > Service detail > Edit
    public ActionResult Edit (int id)
    {
        return View();
    }
}

I want to use MvcSiteMap to define a sitemap of my controllers and actions, to let me generate breadcrumbs and menus.

I have tried using the below decorators to add nodes programmatically, but unfortunately it will not make my tree like I want.

[MvcSiteMapNodeAttribute(Title = "Home"]
[MvcSiteMapNodeAttribute(Title = "Services", ParentKey = "Home")]
[MvcSiteMapNodeAttribute(Title = "Service detail", ParentKey = "Services")]
[MvcSiteMapNodeAttribute(Title = "Edit", ParentKey = "Service detail")]

How can I decorate my actions to make sure the child/parent relations are made how I want?

[HandleError]
public class HomeController : Controller
{
    // Home
    public ActionResult Index ()
    {
        return View();
    }
}

[HandleError]
public class ServiceController : Controller
{
    // Home > Services
    public ActionResult Index ()
    {
        return View();
    }

    // Home > Services > Service detail
    public ActionResult Details (int id)
    {
        return View();
    }

    // Home > Services > Service detail > Edit
    public ActionResult Edit (int id)
    {
        return View();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

冬天的雪花 2024-10-07 23:07:51

您还必须设置密钥:

[MvcSiteMapNodeAttribute(Key = "Home", Title = "Home"]
[MvcSiteMapNodeAttribute(Key = "Services", Title = "Services", ParentKey = "Home")]
[MvcSiteMapNodeAttribute(Key = "ServiceDetail", Title = "Service detail", ParentKey = "Services")]
[MvcSiteMapNodeAttribute(Title = "Edit", ParentKey = "ServiceDetail")]

You'll have to set the key as well:

[MvcSiteMapNodeAttribute(Key = "Home", Title = "Home"]
[MvcSiteMapNodeAttribute(Key = "Services", Title = "Services", ParentKey = "Home")]
[MvcSiteMapNodeAttribute(Key = "ServiceDetail", Title = "Service detail", ParentKey = "Services")]
[MvcSiteMapNodeAttribute(Title = "Edit", ParentKey = "ServiceDetail")]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文