Created
February 8, 2022 11:54
-
-
Save rasmuseeg/bb6e8b491f24e5329e7eb8ff57bf55b7 to your computer and use it in GitHub Desktop.
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
using System; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Mvc; | |
public static class UrlHelperExtensions | |
{ | |
/// <summary> | |
/// Modified QueryString with key to value | |
/// </summary> | |
/// <param name="helper"></param> | |
/// <param name="name"></param> | |
/// <param name="value"></param> | |
/// <returns>?key=value</returns> | |
public static string ModifyQueryString<T>(this UrlHelper helper, string key, T value) | |
{ | |
var request = helper.RequestContext.HttpContext.Request; | |
var nameValues = HttpUtility.ParseQueryString(request.QueryString.ToString()); | |
nameValues.Set(key, value.ToString()); | |
return "?" + nameValues.ToString(); | |
} | |
/// <summary> | |
/// Removes QueryString with key | |
/// </summary> | |
/// <param name="helper"></param> | |
/// <param name="name"></param> | |
/// <param name="value"></param> | |
/// <returns>?key=value</returns> | |
public static string RemoveQueryString(this UrlHelper helper, params string[] keys) | |
{ | |
var request = helper.RequestContext.HttpContext.Request; | |
var nameValues = HttpUtility.ParseQueryString(request.QueryString.ToString()); | |
foreach (var key in keys) | |
nameValues.Remove(key); | |
return "?" + nameValues.ToString(); | |
} | |
/// <summary> | |
/// Has QueryString with key | |
/// </summary> | |
/// <param name="helper"></param> | |
/// <param name="name"></param> | |
/// <param name="value"></param> | |
/// <returns>?key=value</returns> | |
public static bool HasQueryString(this UrlHelper helper, string key) | |
{ | |
var request = helper.RequestContext.HttpContext.Request; | |
var nameValues = HttpUtility.ParseQueryString(request.QueryString.ToString()); | |
var value = nameValues.Get(key); | |
return !string.IsNullOrEmpty(value); | |
} | |
/// <summary> | |
/// Gets the querystring key value with fallback. | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
/// <param name="helper"></param> | |
/// <param name="key">The key of the QueryString</param> | |
/// <param name="fallback">If empty or null</param> | |
/// <returns></returns> | |
public static T GetQueryString<T>(this UrlHelper helper, string key, T fallback) | |
{ | |
var request = helper.RequestContext.HttpContext.Request; | |
var nameValues = HttpUtility.ParseQueryString(request.QueryString.ToString()); | |
if (nameValues.AllKeys.Contains(key)) | |
{ | |
try | |
{ | |
return (T)Convert.ChangeType(nameValues.Get(key), typeof(T)); | |
} | |
catch (Exception) { } | |
} | |
return fallback; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment