w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Write a custom redirect for asp.net that parses the url and check if url contains a string in a string array length = 1000

The easiest way would be to put the redirect mappings into your web.config: http://www.iis.net/configreference/system.webserver/httpredirect

But if you're going to do it in code, why wouldn't you just do something like this in global.asax and have done with it?

protected void Application_BeginRequest()
{
  int    n            = Request.Url.Segments.Length ;
  string postId       = n > 0 ? Request.Url.Segments[n-1] : null ;
  bool   isOldRequest = postId != null &&
RedirectablePaths.Contains(postId) ;

  if ( isOldRequest )
  {
    UriBuilder destination = new UriBuilder( Request.Url ) ;

    destination.Host = "blog." + Request.Url.Host ;

    Response.RedirectPermanent( destination.Uri.AbsoluteUri ) ;
  }

  return ;
}
private static readonly string[] OldBlogPosts = {
  "my-seo-powerful-blog-post",
  "my-other-seo-powerful-blog-post",
  // ...and so, for 960 existings blog posts
  } ;
private static readonly HashSet<string> RedirectablePaths = new
HashSet<string>( OldBlogPosts , StringComparer.OrdinalIgnoreCase ) ;

Edited to note: The reasons for using something like HashSet<T> or SortedSet<T> here

  • Semantics. You have a set here — a unique collection of objects (as opposed to a bag, a collection of objects that allows duplicates and is not necessarily unique) — and you are concerned with set operations: whether or not something is a member of that set.

  • Performance. List<T> is fundamentally an array (not a linked list, its name notwithstanding). Either way, An operation like Enumerable.Any() is an iteration over the collection and runs in O(n) time. That means the the run time is linearly proportional to the size of the data. For Any() to succeed it will take, on the average, n/2 operations to find an item in the set; for Any() to fail, it will require n operations, since it can't fail until its examined everything in the list.

    • HashSet<T> is a hash table and assuming a reasonable hashing function, lookups in the hash table run in O(1) time, meaning that any lookup, whether it succeeds or fails takes [relatively] constant time.

    • SortedSet<T> is an alternative set data structure. Under the covers, it is a *red-black tree, a particular type of height-balanced binary tree. A search in a red-black tree is an O(log n) operation, meaning it's proportional to the logarithm of the data set size.

    Since a red-black tree is a binary tree, it's an O(log2 n) operation to be precise, and therefore proportional to the depth of the tree.

So, if you have a big set, say, of 4.2 billion items,

  • Stored in a List<T>, an operation like Any() will take, on the average 2.1 billion comparisons to succeed and return true and 4.2 billion comparisons to fail and return false.

  • Stored in a SortedSet<T>, that equivalent Contains() operation will, in the worst case (item not found), require c. 32 comparisons — plus/minus a few, since red-black trees aren't perfectly balanced — to find (or not find) an item.

  • Stored in a HashSet<T>, the lookup will run in O(1) [relatively] constant time, but whether that is faster than the SortedSet<T>'s O(log n) time or not is is dependent on several factors, including:

    • complexity of the hash function (cost of computing the hash value)
    • quality of the computed hash value (how perfectly random is it?)
    • number of buckets in the hash table
    • number of objects per bucket.




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.