Skip to content

Instantly share code, notes, and snippets.

@DTTerastar
Last active September 27, 2016 20:53
Show Gist options
  • Save DTTerastar/4502856 to your computer and use it in GitHub Desktop.
Save DTTerastar/4502856 to your computer and use it in GitHub Desktop.
public static string ResolveUrl(string url)
{
string appDomainAppVirtualPath = HttpRuntime.AppDomainAppVirtualPath;
return url.StartsWith("~/") ? appDomainAppVirtualPath + url.Substring(appDomainAppVirtualPath != null && appDomainAppVirtualPath.EndsWith("/") ? 2 : 1) : url;
}
public static string ResolveUrl(string url, object queryStringParams, bool skipEmptyValues = true)
{
if (queryStringParams == null) return ResolveUrl(url);
var sb = new StringBuilder(ResolveUrl(url));
var firstParam = !url.Contains("?");
foreach (PropertyDescriptor p in TypeDescriptor.GetProperties(queryStringParams))
{
var value = p.GetValue(queryStringParams);
if (value == null) continue;
var valueToString = value.ToString();
if (string.IsNullOrEmpty(valueToString) && skipEmptyValues) continue;
sb.AppendFormat(firstParam ? "?{0}{1}{2}" : "&{0}{1}{2}", p.Name.UrlEncode(), '=', valueToString.UrlEncode());
firstParam = false;
}
return sb.ToString();
}
public static string GlobalResolveUrl(string url, object queryStringParams)
{
var relativeUri = ResolveUrl(url, queryStringParams);
if (relativeUri == null)
return null;
var uri = new UriBuilder(new Uri(HttpContext.Current.Request.Url, relativeUri));
return uri.Uri.GetLeftPart(UriPartial.Query);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment