获取参数值,不存在时返回预设值

///帮助类

 public sealed class RequestHelper
    {

///获取参数值,不存在的时候返回默认值 

public static T Get<T>(string name, T defaultValue)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name is null!");
            }

            object value = HttpContext.Current.Request[name];

            if (value != null)
            {
                return (T)value;
            }
            else
            {
                return defaultValue;
            }
        }

     }

eg.

   string target = RequestHelper.Get("target", "").ToLower();

posted @ 2013-01-11 13:43  春天里的桃花  阅读(137)  评论(0编辑  收藏  举报