patterncsharpMinor
Query String Serializer
Viewed 0 times
serializerquerystring
Problem
I have a ASP.NET Web Forms project, and I want to build calls to .aspx pages in a strongly-typed fashion. I ended up rolling my own serializer that takes simple structs and saves them to/loads them from the query string. What do you think? Is my approach sane? Is there an accepted alternative I don't know about? Any feedback on the code?
Here's what building a call to a particular page looks like:
FooPage:
Serialize/Deserialize to/from NameValueCollection:
```
public static void SerializeToNameValueCollection(NameValueCollection nameValueCollection, T @object) where T : struct
{
Type type = typeof(T);
var fields = type.GetFields();
foreach (var field in fields)
{
string key = field.Name;
var value = field.GetValue(@object);
if (value != null)
nameValueCollection.Add(key, value.ToString());
}
}
public static T DeserializeFromNameValueCollection(NameValueCollection nameValueCollection) where T : struct
{
T result = new T();
Type type = typeof(T);
var fields = type.GetFields();
foreach (var field in fields)
{
string key = field.Name;
string stringValue = nameValueCollection[key];
if (stringValue != null)
{
object value;
Here's what building a call to a particular page looks like:
var fooParams= new FooPage.Parameters
{
NodeID = nodeId,
FooString = "the foo string"
};
string url = MyHelper.BuildCall(FooPage.URL, fooParams);
//url: ~/dir/FooPage.aspx?NodeID=5&FooString=the%20foo%20stringFooPage:
public partial class FooPage : System.Web.UI.Page
{
public const string URL = "~/Dir/FooPage.aspx";
public struct Parameters
{
public long? NodeID;
public string FooString;
public int? OtherParam;
}
protected Parameters Params;
protected void Page_Load(object sender, EventArgs e)
{
Params = MyHelper.DeserializeFromNameValueCollection(Request.Params);
//...
//use Params.NodeID, Params.FooString, etc..
}
}Serialize/Deserialize to/from NameValueCollection:
```
public static void SerializeToNameValueCollection(NameValueCollection nameValueCollection, T @object) where T : struct
{
Type type = typeof(T);
var fields = type.GetFields();
foreach (var field in fields)
{
string key = field.Name;
var value = field.GetValue(@object);
if (value != null)
nameValueCollection.Add(key, value.ToString());
}
}
public static T DeserializeFromNameValueCollection(NameValueCollection nameValueCollection) where T : struct
{
T result = new T();
Type type = typeof(T);
var fields = type.GetFields();
foreach (var field in fields)
{
string key = field.Name;
string stringValue = nameValueCollection[key];
if (stringValue != null)
{
object value;
Solution
One of my favorite patterns for handling URL parameters in WebForms is the WebNavigator - http://polymorphicpodcast.com/shows/webnavigator/
If you're going through these kinds of Strongly-typed interactions for passing parameters between pages, maybe it is time you check out ASP .NET MVC - your solution looks a lot like model-binding.
If you're going through these kinds of Strongly-typed interactions for passing parameters between pages, maybe it is time you check out ASP .NET MVC - your solution looks a lot like model-binding.
Context
StackExchange Code Review Q#10234, answer score: 2
Revisions (0)
No revisions yet.