SiteMap Navigation and Query String

I'm currently trying to figure out how to add dynamic query string parameters to my map navigation menu. For example, the user selects the source and version with which he wants to work. I have a simple site map that creates navigation links, but the parameters that the user selected must be passed in the query string. The default map is as follows:

<siteMapNode url="" title=""  description="" >
   <siteMapNode url="~/Image.aspx?location=Our Products" title="Our Products" description="Our Products" />
   <siteMapNode url="~/Headline.aspx?location=Our Authors" title="Our Authors"  description="Our Authors" />
</siteMapNode>

Now the links will have to be dynamically added depending on what was selected by the user. For instance:

<siteMapNode url="~/Image.aspx?location=Our Products&Source=12345&Edition=asdfff" title="Our Products"  description="Our Products" />
<siteMapNode url="~/Headline.aspx?location=Our Authors&Source=12345&Edition=asdfff" title="Our Authors"  description="Our Authors" />

Hope this is pretty clear. Let me know if anyone needs a deeper explanation.

thank

+3
source share
3

, . SiteMap.SiteMapResolve Global.asax, URL- SiteMapProvider.FindSiteMapNode URL:

private void Application_Start(object sender, EventArgs e)
{
    SiteMap.SiteMapResolve += ResolveCustomNodes;
}

private SiteMapNode ResolveCustomNodes(object sender, SiteMapResolveEventArgs e)
{
    // catch ~/Image.aspx and ~/Headline.aspx
    if (e.Context.Request.AppRelativeCurrentExecutionFilePath.Equals(
        "~/Image.aspx", StringComparison.OrdinalIgnoreCase)
      || e.Context.Request.AppRelativeCurrentExecutionFilePath.Equals(
        "~/Headline.aspx", StringComparison.OrdinalIgnoreCase))
    {
        string location = context.Request.QueryString["location"];
        if (location != null) // ignore everything except location=
            return e.Provider.FindSiteMapNode(
                e.Context.Request.AppRelativeCurrentExecutionFilePath
                "?location=" + HttpUtility.UrlEncode(location));
    }
    return null; // use default implementation;
}

SiteMapProvider s, .

, , , ( ):

<siteMapNode> , . , sitemap ( , ). -

<siteMapNode url="~/Image.aspx?location=Our Products"
             queryStringField="location"
             title="Our Products" description="Our Products" />

, node, queryStringField:

private IEnumerable<SiteMapNode> FindNodesWithQueryString(SiteMapNode node)
{
    if (node["queryStringField"] != null)
        yield return node;
    foreach (SiteMapNode childNode in node.ChildNodes)
    {
        foreach (SiteMapNode matchingNode in FindNodesWithQueryString(childNode))
        {
            yield return matchingNode;
        }
    }
}

. , , , , SiteMapResolve , . SiteMapProvider s.

private SiteMapNode ResolveCustomNodes(object sender, SiteMapResolveEventArgs e)
{
    string path = e.Context.Request.AppRelativeCurrentExecutionFilePath;
    foreach (var candidate in from node in FindNodesWithQueryString(
                                                              SiteMap.RootNode)
                              select new {
                                  Url = node.Url,
                                  UrlNoQuery = node.Url.Split('?')[0],
                                  QueryStringField = node["queryStringField"],
                                  Node = node
                              } into x
                              where path.Equals(x.UrlNoQuery, 
                                             StringComparison.OrdinalIgnoreCase)
                              select x)
    {
        string paramValue = context.Request.QueryString[
                                                    candidate.QueryStringField];

        if (paramValue != null)
        {
            string url = candidate.UrlNoQuery + "?" + candidate.QueryStringField
                       + "=" + HttpUtility.UrlEncode(paramValue);
            if (url.Equals(candidate.Url, StringComparison.OrdinalIgnoreCase))
                return candidate.Node;
        }   
    }
    return null;
}
+10

I had a similar problem with using base64 string, url encoded as a request parameter on a sitemap. The simplest solution is to simply handle the MenuItemDataBound event and make your own encoding there:

    string[] url = e.Item.NavigateUrl.Split('?');

    if (url.Length == 2)
    {
        url[1] = WebUtils.encodeString(url[1]);
    }

    e.Item.NavigateUrl = string.Join("?", url);

My webutils method:

    public static string encodeString(string value)
{
    return HttpUtility.UrlEncode(Convert.ToBase64String(Encoding.UTF8.GetBytes(value)));
}

public static string decodeString(string value)
{
    return Encoding.UTF8.GetString(Convert.FromBase64String(HttpUtility.UrlDecode(value)));
}
0
source

Source: https://habr.com/ru/post/1751662/


All Articles