Sprache: C#
Aus URI Objekt den Parameter Value per Extension-Method zurückgeben
public static string ParameterValue(this Uri uri, string paramName)
{
if (uri != null && uri.Query != null && uri.Query.Length > 0 && uri.Query.ToLower().Contains(paramName))
{
Match m = Regex.Match(uri.Query, "^?" + paramName + "=([^&#]*)");
if(m.Success) return m.Value.Replace(paramName + "=", "");
}
return "";
}
public static string ParameterValue(this Uri uri, string paramName)
{
if (uri != null && uri.Query != null && uri.Query.Length > 0 && uri.Query.ToLower().Contains(paramName))
{
Match m = Regex.Match(uri.Query, "^?" + paramName + "=([^&#]*)");
if(m.Success) return m.Value.Replace(paramName + "=", "");
}
return "";
}
Alte URL:
/snippet/parameter-value-aus-url-per-extension-method-ermitteln/1325
… oder ohne RegEx über die HttpUtility-Klasse im Namespace System.Web
[code]private static string ParameterValue(this Uri uri, string paramName)
{
return HttpUtility.ParseQueryString(uri.Query)[paramName] ?? string.Empty;
}[/code]