Last active
December 22, 2015 16:19
-
-
Save DTTerastar/6498890 to your computer and use it in GitHub Desktop.
MapRedirect for temp and perm redirecting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class RouteCollectionExtensions | |
{ | |
public static void MapRedirect(this RouteCollection rc, string url, string dest, bool perm = false) | |
{ | |
rc.Add(new Route(url, new RedirectRouteHandler(dest, perm))); | |
} | |
} | |
public class VirtualPathRouteHandlerBase | |
{ | |
private static readonly Regex Regex = new Regex(@"{(\w+)}"); | |
private string _virtualPath; | |
private IList<string> _parsed; | |
public VirtualPathRouteHandlerBase(string virtualPath) | |
{ | |
VirtualPath = virtualPath; | |
} | |
public string VirtualPath | |
{ | |
get { return _virtualPath; } | |
set | |
{ | |
_virtualPath = value; | |
_parsed = SplitUrlToPathSegmentStrings(value); | |
} | |
} | |
public string BuildVirtualPath(RequestContext requestContext) | |
{ | |
var sb = new StringBuilder(); | |
foreach (string list in _parsed) | |
sb.Append(Regex.Replace(list, a => | |
{ | |
object v; | |
if (requestContext.RouteData.Values.TryGetValue(a.Groups[1].Value, out v)) | |
return v + ""; | |
throw new Exception(string.Format("{0} not found in request context", a.Value)); | |
})); | |
return sb.ToString(); | |
} | |
internal static IList<string> SplitUrlToPathSegmentStrings(string url) | |
{ | |
var list = new List<string>(); | |
if (!string.IsNullOrEmpty(url)) | |
{ | |
int index; | |
for (int i = 0; i < url.Length; i = index + 1) | |
{ | |
index = url.IndexOf('/', i); | |
if (index == -1) | |
{ | |
string str = url.Substring(i); | |
if (str.Length > 0) | |
list.Add(str); | |
return list; | |
} | |
string item = url.Substring(i, index - i); | |
if (item.Length > 0) | |
{ | |
list.Add(item); | |
} | |
list.Add("/"); | |
} | |
} | |
return list; | |
} | |
} | |
public class RedirectRouteHandler : VirtualPathRouteHandlerBase, IRouteHandler | |
{ | |
private readonly bool _perm; | |
public RedirectRouteHandler(string virtualPath, bool perm) | |
: base(virtualPath) | |
{ | |
_perm = perm; | |
} | |
public IHttpHandler GetHttpHandler(RequestContext requestContext) | |
{ | |
return new RedirectHttpHandler(BuildVirtualPath(requestContext), _perm); | |
} | |
} | |
public class RedirectHttpHandler : IHttpHandler | |
{ | |
private readonly string _virtualPath; | |
private readonly bool _perm; | |
public RedirectHttpHandler(string virtualPath, bool perm) | |
{ | |
_virtualPath = virtualPath; | |
_perm = perm; | |
} | |
public void ProcessRequest(HttpContext context) | |
{ | |
context.Response.Redirect(_virtualPath, !_perm); | |
context.Response.StatusCode = 301; | |
context.Response.End(); | |
} | |
public bool IsReusable | |
{ | |
get { return false; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment