在asp.net中动态构建SiteMapPath

发布于 2024-09-24 17:36:15 字数 502 浏览 5 评论 0原文

我正在尝试使用 SiteMapPath 在我的网站上构建动态站点地图。

应该是这样的:

Home > Products > %product_name% > Prices

其中 %product_name% 在运行时动态设置,具体取决于用户的选择。

我读过很多关于该主题的文章并选择这个 http://harriyott.com/2007/03/adding-dynamic-nodes-to-aspnet-site.aspx。它动态更改 web.sitemap XML 文件。问题是它仍然在开始时仅构建一次站点地图,然后在每个页面上使用它。

我怎样才能让它在每个加载的页面上重建?

I'm trying to build a dynamic site map on my site using SiteMapPath.

Should be like this:

Home > Products > %product_name% > Prices

where %product_name% is set dynamically in the runtime, depending on the user's choice.

I've read many articles on the theme and choose this http://harriyott.com/2007/03/adding-dynamic-nodes-to-aspnet-site.aspx. It dynamically changes the web.sitemap XML file. The problem is that it still builds the sitemap only once in the beginning and then uses it on each page.

How can I make it to rebuild on each loaded page?

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

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

发布评论

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

评论(2

も让我眼熟你 2024-10-01 17:36:15

试试这个:

右键单击您的项目“添加新项目”,然后选择“站点地图”,
它将具有如下所示的 XML 结构:

<?xml version="1.0" encoding="utf-8" ?>

     <siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >

       <siteMapNode url="~/Default.aspx" title="Home " description="">

         <siteMapNode url="~/the page URL" title="Products"  description="" >

             <siteMapNode url="~/the page URL" title=" %product_name%"  description="" >

                 <siteMapNode url="~/the page URL" title="Prices"  description="" />

             </siteMapNode >

         </siteMapNode >

       </siteMapNode >

     <sitemap>

** 为每个节点添加描述是可选的。

现在您需要将其放置在您想要的位置,因此您在页面的 HTML 一侧添加了以下代码:

<asp:SiteMapPath ID="SiteMapPath1" runat="server">

<CurrentNodeStyle CssClass="Some class" />

   <PathSeparatorTemplate>

      <img runat="server" alt="" src="an image to separate between nodes" height="5" width="5" />

   </PathSeparatorTemplate>

</asp:SiteMapPath>

当然,您有两个页面 - 一个用于产品,一个用于价格。

为SiteMap中的某个节点动态分配Tile;在价格页面中添加此代码:

1)在页面加载中:

SiteMap.SiteMapResolve += new SiteMapResolveEventHandler(SiteMap_SiteMapResolve);

2)在同一页面(价格页面)中添加此功能:

 SiteMapNode SiteMap_SiteMapResolve(object sender, SiteMapResolveEventArgs e)
{
    SiteMapNode currentNode = SiteMap.CurrentNode.Clone(true);
    SiteMapNode tempNode = currentNode;

    tempNode.ParentNode.Title = "Change the Product name";
    tempNode.ParentNode.Url = "Change the Product url";

    return currentNode;
}

如您所见,您可以根据需要操作父节点,更改标题,url,等等。我想你也想改变网址;例如:“product.aspx?ID=blah”

Try this:

Right click on your project "add new item" then choose "Site Map",
it will have an XML structure that looks like:

<?xml version="1.0" encoding="utf-8" ?>

     <siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >

       <siteMapNode url="~/Default.aspx" title="Home " description="">

         <siteMapNode url="~/the page URL" title="Products"  description="" >

             <siteMapNode url="~/the page URL" title=" %product_name%"  description="" >

                 <siteMapNode url="~/the page URL" title="Prices"  description="" />

             </siteMapNode >

         </siteMapNode >

       </siteMapNode >

     <sitemap>

** adding description for each node is optional.

Now you need to place it where you want, so you add this code in the HTML side of the page:

<asp:SiteMapPath ID="SiteMapPath1" runat="server">

<CurrentNodeStyle CssClass="Some class" />

   <PathSeparatorTemplate>

      <img runat="server" alt="" src="an image to separate between nodes" height="5" width="5" />

   </PathSeparatorTemplate>

</asp:SiteMapPath>

Of course you have two pages - one for product and one for prices.

To assign Tile dynamically for some node in the SiteMap; add this code in the Prices Page:

1) In the page load:

SiteMap.SiteMapResolve += new SiteMapResolveEventHandler(SiteMap_SiteMapResolve);

2) Add this function in the same page (prices page):

 SiteMapNode SiteMap_SiteMapResolve(object sender, SiteMapResolveEventArgs e)
{
    SiteMapNode currentNode = SiteMap.CurrentNode.Clone(true);
    SiteMapNode tempNode = currentNode;

    tempNode.ParentNode.Title = "Change the Product name";
    tempNode.ParentNode.Url = "Change the Product url";

    return currentNode;
}

As you can see you can manipulate the parent Node as you want, change the title, the url, etc. I think you want to change the url too; for example: "product.aspx?ID=blah"

纵山崖 2024-10-01 17:36:15

伟大的!
如果有人想要在 vb 中实现相同的功能,这里是代码:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    AddHandler SiteMap.SiteMapResolve, AddressOf Me.SiteMap_SiteMapResolve

End Sub

Private Function SiteMap_SiteMapResolve(sender As Object, e As SiteMapResolveEventArgs) As SiteMapNode
    Dim currentNode As SiteMapNode = SiteMap.CurrentNode.Clone(True)
    Dim tempNode As SiteMapNode = currentNode

    tempNode.ParentNode.Title = "Change the Product name"
    tempNode.ParentNode.Url = "Change the Product url"

    Return currentNode
End Function

Great!
In case of someone wants the same in vb here is the code:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    AddHandler SiteMap.SiteMapResolve, AddressOf Me.SiteMap_SiteMapResolve

End Sub

Private Function SiteMap_SiteMapResolve(sender As Object, e As SiteMapResolveEventArgs) As SiteMapNode
    Dim currentNode As SiteMapNode = SiteMap.CurrentNode.Clone(True)
    Dim tempNode As SiteMapNode = currentNode

    tempNode.ParentNode.Title = "Change the Product name"
    tempNode.ParentNode.Url = "Change the Product url"

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