QueryString的解析
QueryString的解析
HttpQueryStringAttribute.cs

Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication1


{

/**////
/// Specifies a field for a query string.
///
[AttributeUsage(AttributeTargets.Field)]
public sealed class HttpQueryStringAttribute : Attribute

{
private string _name;
private object _defaultValue;
private bool _isRequired;


/**////
/// Constructor. The query string must be provided.
///
/// Name of the query string
public HttpQueryStringAttribute(string name)

{
_name = name;
_defaultValue = null;
_isRequired = true;
}


/**////
/// Constructor. If the query string is not be provided, using the default value.
///
/// Name of the query string
/// Default value of the query string which is not provided
public HttpQueryStringAttribute(string name, object defaultValue)

{
_name = name;
_defaultValue = defaultValue;
_isRequired = false;
}


/**////
/// Name of the query string.
///
public string Name

{

get
{ return _name; }
}


/**////
/// Default value of the query string which is not provided.
///
public object DefaultValue

{

get
{ return _defaultValue; }
}


/**////
/// Indicates if the query string must be provided.
///
public bool IsRequired

{

get
{ return _isRequired; }
}

internal static string Parse()

{
throw new NotImplementedException();
}
}

}
PageBase.cs

Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;

namespace WebApplication1


{
public class PageBase : System.Web.UI.Page

{

/**////
/// Override OnLoad method of base class.
///
///
protected override void OnLoad(System.EventArgs e)

{
ParameterInitialize();
base.OnLoad(e);
}


/**////
/// Initialize parameters according to query strings.
///
private void ParameterInitialize()

{
// Get Type of current page class.
Type type = this.GetType();

// Get all fields of current page class.
FieldInfo[] fields = type.GetFields();

foreach (FieldInfo field in fields)

{
// Get HttpQueryStringAttribute of current field.
HttpQueryStringAttribute attribute = (HttpQueryStringAttribute)Attribute.GetCustomAttribute(field, typeof(HttpQueryStringAttribute));

// If has HttpQueryStringAttribute, this field is for a query string.
if (attribute != null)

{
SetField(field, attribute);
}
}
}


/**////
/// Set field according to the HttpQueryStringAttribute.
///
/// The field will be set
/// The attribute of current field
private void SetField(FieldInfo field, HttpQueryStringAttribute attribute)

{
// The query string must be provided.
if (attribute.IsRequired)

{
if (Request.QueryString[attribute.Name] != null)

{
SetFieldValue(field, this, attribute.Name, field.FieldType);
}
else

{

throw new Exception(string.Format("Query string \"
{0}\" is required", attribute.Name), new NullReferenceException());
}
}
// If the query string is not be provided, using the default value.
else

{
if (attribute.DefaultValue == null || field.FieldType == attribute.DefaultValue.GetType())

{
if (Request.QueryString[attribute.Name] == null || Request.QueryString[attribute.Name] == string.Empty)

{
field.SetValue(this, attribute.DefaultValue);
}
else

{
SetFieldValue(field, this, attribute.Name, field.FieldType);
}
}
else

{

throw new Exception(string.Format("Invalid default value of query string \"
{0}\"({1})", attribute.Name, field.Name), new NullReferenceException());
}
}
}


/**////
/// Set the value of current field according to the query string.
///
/// The field will be set
/// The object whose field value will be set
/// The name of query string
/// The type to be converted
private void SetFieldValue(FieldInfo field, object obj, string name, Type conversionType)

{
try

{
// Set field value.
field.SetValue(obj, Convert.ChangeType(Request.QueryString[name], conversionType));
}
catch (Exception ex)

{

throw new Exception(string.Format("The given value of query string \"
{0}\" can not be convert to {1}", name, conversionType), ex);
}
}
}
}
Defaut.aspx.cs

Code
public partial class _Default : PageBase

{

/**////
/// Name 是Query String的名字,"Anonymous"是缺省值。
/// 如果没有提供这个Query String,就采用缺省值。
///
[HttpQueryString("Name", "Anonymous")]
public string name;


/**////
/// UserId 是Query String的名字,不提供缺省值。
/// 如果没有提供这个Query String或者提供的格式不正确导致转换失败,都会抛出异常。
///
[HttpQueryString("UserId")]
public int userId;

protected void Page_Load(object sender, EventArgs e)

{
Response.Write(string.Format("Name is {0}", name));
Response.Write(string.Format("UserId is {0}", userId));
}

}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication1

{
/**////
/// Specifies a field for a query string.
///
[AttributeUsage(AttributeTargets.Field)]
public sealed class HttpQueryStringAttribute : Attribute
{
private string _name;
private object _defaultValue;
private bool _isRequired;

/**////
/// Constructor. The query string must be provided.
///
/// Name of the query string
public HttpQueryStringAttribute(string name)
{
_name = name;
_defaultValue = null;
_isRequired = true;
}

/**////
/// Constructor. If the query string is not be provided, using the default value.
///
/// Name of the query string
/// Default value of the query string which is not provided
public HttpQueryStringAttribute(string name, object defaultValue)
{
_name = name;
_defaultValue = defaultValue;
_isRequired = false;
}

/**////
/// Name of the query string.
///
public string Name
{
get
{ return _name; }
}

/**////
/// Default value of the query string which is not provided.
///
public object DefaultValue
{
get
{ return _defaultValue; }
}

/**////
/// Indicates if the query string must be provided.
///
public bool IsRequired
{
get
{ return _isRequired; }
}
internal static string Parse()
{
throw new NotImplementedException();
}
}
}PageBase.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
namespace WebApplication1

{
public class PageBase : System.Web.UI.Page
{
/**////
/// Override OnLoad method of base class.
///
///
protected override void OnLoad(System.EventArgs e)
{
ParameterInitialize();
base.OnLoad(e);
}

/**////
/// Initialize parameters according to query strings.
///
private void ParameterInitialize()
{
// Get Type of current page class.
Type type = this.GetType();
// Get all fields of current page class.
FieldInfo[] fields = type.GetFields();
foreach (FieldInfo field in fields)
{
// Get HttpQueryStringAttribute of current field.
HttpQueryStringAttribute attribute = (HttpQueryStringAttribute)Attribute.GetCustomAttribute(field, typeof(HttpQueryStringAttribute));
// If has HttpQueryStringAttribute, this field is for a query string.
if (attribute != null)
{
SetField(field, attribute);
}
}
}

/**////
/// Set field according to the HttpQueryStringAttribute.
///
/// The field will be set
/// The attribute of current field
private void SetField(FieldInfo field, HttpQueryStringAttribute attribute)
{
// The query string must be provided.
if (attribute.IsRequired)
{
if (Request.QueryString[attribute.Name] != null)
{
SetFieldValue(field, this, attribute.Name, field.FieldType);
}
else
{
throw new Exception(string.Format("Query string \"
{0}\" is required", attribute.Name), new NullReferenceException());
}
}
// If the query string is not be provided, using the default value.
else
{
if (attribute.DefaultValue == null || field.FieldType == attribute.DefaultValue.GetType())
{
if (Request.QueryString[attribute.Name] == null || Request.QueryString[attribute.Name] == string.Empty)
{
field.SetValue(this, attribute.DefaultValue);
}
else
{
SetFieldValue(field, this, attribute.Name, field.FieldType);
}
}
else
{
throw new Exception(string.Format("Invalid default value of query string \"
{0}\"({1})", attribute.Name, field.Name), new NullReferenceException());
}
}
}

/**////
/// Set the value of current field according to the query string.
///
/// The field will be set
/// The object whose field value will be set
/// The name of query string
/// The type to be converted
private void SetFieldValue(FieldInfo field, object obj, string name, Type conversionType)
{
try
{
// Set field value.
field.SetValue(obj, Convert.ChangeType(Request.QueryString[name], conversionType));
}
catch (Exception ex)
{
throw new Exception(string.Format("The given value of query string \"
{0}\" can not be convert to {1}", name, conversionType), ex);
}
}
}
}
public partial class _Default : PageBase
{
/**////
/// Name 是Query String的名字,"Anonymous"是缺省值。
/// 如果没有提供这个Query String,就采用缺省值。
///
[HttpQueryString("Name", "Anonymous")]
public string name;

/**////
/// UserId 是Query String的名字,不提供缺省值。
/// 如果没有提供这个Query String或者提供的格式不正确导致转换失败,都会抛出异常。
///
[HttpQueryString("UserId")]
public int userId;
protected void Page_Load(object sender, EventArgs e)
{
Response.Write(string.Format("Name is {0}", name));
Response.Write(string.Format("UserId is {0}", userId));
}
}
浙公网安备 33010602011771号