ASP.NET Key / Value List

I am making a custom 404 page for a large website that is being redesigned. There are about 40 high-usage pages that can be marked by customers, and our new site structure will violate these bookmarks.

On my 404 page, I want to warn them of a new URL if they try to go to one of these high-usage pages through their old URL. So, I have a couple of dynamic controls on page 404, one for "did-you-want-this?". type of dialog and the other is the if-so-go-here (and refresh your bookmark) dialog box. This is the easy part.

To suggest a new URL, I am looking at the requested URL. If it has keywords, I’m going to propose a new URL based on this, and I will take away the relevant needs ... and if so ... suggestions at 404 as indicated above.

So, I want to keep these 40-ish key / value pairs (keyword / new URL pairs) in the data structure, and I'm not sure what would be better. Dictionary? IDictionary? What is the difference and what is more appropriate?

Or am I completely mistaken?

Thank you for your time.

+3
source share
5 answers

I would use the dictionary <T, T> from the System.Collections.Generic namespace.

+4
source

You can use NameValueCollection.

+2
source

, , , , Uri . , .

class UriSuggester {
   private List<SuggestedUri> Uris { get; set; }

   Uri[] GetSuggestions(Uri originalUri) {
      var suggestionHits = new Dictionary<SuggestedUri, int>();
      foreach (var keyword in KeyWords.Parse(originalUri)) {
         // find suggestions matching that keyword
         foreach (var suggestedUri in Uris.Where(u => u.Keywords.Contains(keyword)) {
           // add a hit for keyword match
           suggestionHits[suggestedUri] += 1;
         }
      }
      // order by weight * hits
      return suggestionHits.Keys
        .OrderBy(s => s.Weight * suggestionHits[s])
        .Select(s => s.Uri)
        .ToArray();
   }
}

class SuggestedUri {
   public Uri Suggested { get; set; }
   public int Weight { get; set; }
   public Keyword[] Keywords;
}

class Keyword {
   public string Value { get; set; }
   public static Keyword[] Parse(Uri uri);
   override Equals;
   override GetHashCode;
}
+2
source

The dictionary will be fine. Wether you store it, since the IDictionary interface type or the dictionary itself does not matter much in this case, since it will not go around much, in addition, on the 404 page itself.

Have you considered rewriting URLs to support old URLs?

+1
source

You can write your own class logic and then assign it to the list data structure as follows:

public class KeyValuesClass
{
    private string a_key;
    private string a_value;

    public KeyValuesClass(string a_key, string a_value)
    {
           this.a_key = a_key;
           this.a_value = a_value;
    }

    public string Key
    {
        get{ return a_key; }
        set { a_key = value; }
    }

    public string Value
    {
        get{ return a_value; }
        set { a_value = value; }
    }

}

somewhere in code

List<KeyValuesClass> my_key_value_list = new List<KeyValuesClass>();
my_key_value_list.Add(new KeyValuesClass("key", "value");

But you can consider the Dictionary as our programmer mentioned above :)

+1
source

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


All Articles