Created
September 17, 2015 12:03
-
-
Save ststeiger/702ec4b6c1f100b1b794 to your computer and use it in GitHub Desktop.
Get Parameter from POST/GET/HEADER for ASP.NET vNext
This file contains hidden or 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
| namespace AspNetVnextUtils | |
| { | |
| public static class ParameterExtensions | |
| { | |
| private static System.Reflection.FieldInfo InitFieldInfo() | |
| { | |
| System.Type t = typeof(System.Collections.Specialized.NameObjectCollectionBase); | |
| System.Reflection.FieldInfo fi = t.GetField("_entriesTable", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); | |
| if(fi == null) // Mono | |
| fi = t.GetField("m_ItemsContainer", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); | |
| return fi; | |
| } | |
| private static System.Reflection.FieldInfo m_fi = InitFieldInfo(); | |
| public static bool ContainsKey(this System.Collections.Specialized.NameValueCollection nvc, string key) | |
| { | |
| //System.Collections.Specialized.NameValueCollection nvc = new System.Collections.Specialized.NameValueCollection(); | |
| //nvc.Add("hello", "world"); | |
| //nvc.Add("test", "case"); | |
| // The Hashtable is case-INsensitive | |
| System.Collections.Hashtable ent = (System.Collections.Hashtable)m_fi.GetValue(nvc); | |
| return ent.ContainsKey(key); | |
| } // End Function ContainsKey | |
| //private static bool ContainsKey(System.Collections.Specialized.NameValueCollection nvc, string key) | |
| //{ | |
| // foreach (string str in nvc.AllKeys) | |
| // { | |
| // if (System.StringComparer.InvariantCultureIgnoreCase.Equals(str, key)) | |
| // return true; | |
| // } | |
| // return false; | |
| //} // End Function ContainsKey | |
| public static bool ContainsParameter(System.Web.HttpContext context, string name) | |
| { | |
| return ContainsKey(context.Request.QueryString, name) | |
| || ContainsKey(context.Request.Form, name) | |
| || ContainsKey(context.Request.Headers, name) | |
| || ContainsKey(context.Request.ServerVariables, name) | |
| ; | |
| } | |
| public static string GetParameter(System.Web.HttpContext context, string name) | |
| { | |
| bool exists = false; | |
| return GetParameter(context, name, ref exists); | |
| } | |
| public static string GetParameter(System.Web.HttpContext context, string name, ref bool exists) | |
| { | |
| // System.Collections.Specialized.NameValueCollection nvc = new System.Collections.Specialized.NameValueCollection(); | |
| //nvc.Add(context.Request.QueryString); | |
| //nvc.Add(context.Request.Form); | |
| //nvc.Add(context.Request.Headers); | |
| //nvc.Add(context.Request.ServerVariables); | |
| if (ContainsKey(context.Request.QueryString, name)) | |
| { | |
| exists = true; | |
| return context.Request.QueryString[name]; | |
| } | |
| if (ContainsKey(context.Request.Form, name)) | |
| { | |
| exists = true; | |
| return context.Request.Form[name]; | |
| } | |
| if (ContainsKey(context.Request.Headers, name)) | |
| { | |
| exists = true; | |
| return context.Request.Headers[name]; | |
| } | |
| if (ContainsKey(context.Request.ServerVariables, name)) | |
| { | |
| exists = true; | |
| return context.Request.ServerVariables[name]; | |
| } | |
| //foreach (var key in context.Request.QueryString.AllKeys) | |
| //{ | |
| // string[] vals = context.Request.QueryString.GetValues(key); | |
| //} | |
| //foreach (var key in context.Request.Form.AllKeys) | |
| //{ | |
| // string[] vals = context.Request.Form.GetValues(key); | |
| //} | |
| //foreach (var key in context.Request.Headers.AllKeys) | |
| //{ | |
| // string[] vals = context.Request.Headers.GetValues(key); | |
| //} | |
| //foreach (var key in context.Request.ServerVariables.AllKeys) | |
| //{ | |
| // string[] vals = context.Request.ServerVariables.GetValues(key); | |
| //} | |
| // foreach(var x in context.Request.Cookies.AllKeys) {} | |
| return null; | |
| } // End Function GetParameter | |
| } // End static class ParameterExtensions | |
| } // End Namespace AspNetVnextUtils | |
| Public NotInheritable Class ParameterExtensions | |
| Private Shared Function InitFieldInfo() As System.Reflection.FieldInfo | |
| Dim t As System.Type = GetType(System.Collections.Specialized.NameObjectCollectionBase) | |
| Dim fi As System.Reflection.FieldInfo = t.GetField("_entriesTable", System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.NonPublic) | |
| If fi Is Nothing Then ' Mono | |
| fi = t.GetField("m_ItemsContainer", System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.NonPublic) | |
| End If | |
| Return fi | |
| End Function | |
| Private Shared m_fi As System.Reflection.FieldInfo = InitFieldInfo() | |
| ' <System.Runtime.CompilerServices.Extension> _ | |
| Public Shared Function ContainsKey(nvc As System.Collections.Specialized.NameValueCollection, key As String) As Boolean | |
| ' The Hashtable is case-INsensitive | |
| Dim ent As System.Collections.Hashtable = DirectCast(m_fi.GetValue(nvc), System.Collections.Hashtable) | |
| Return ent.ContainsKey(key) | |
| End Function ' ContainsKey | |
| End Class | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment