示例#1
0
		private object TryGetArg(IHttpContext context, ParameterInfo param, IDictionary<string, string> urlArgs,
			IDictionary<string, string> routeArgs, IQueryValidator argResolver)
		{
			var type = param.ParameterType;

			if (type == typeof(IHttpContext))
				return context;

			var paramName = param.Name.ToLower();

			string argValue;
			var gotValue = urlArgs.TryGetValue(paramName, out argValue)
				|| (routeArgs != null && routeArgs.TryGetValue(paramName, out argValue));

			if (!gotValue)
			{
				object defaultValue;
				if (param.TryGetDefaultValue(out defaultValue))
					return defaultValue;

				if (Nullable.GetUnderlyingType(param.ParameterType) != null)
					return null;

				return Error(context, "Missing param '{0}'", paramName);
			}

			if (string.IsNullOrEmpty(argValue))
				return Error(context, "Empty param '{0}'", paramName);

			object parsedArg;
			if (!m_argParser.TryParse(type, argValue, out parsedArg))
				return Error(context, "Param '{0}' is invalid", paramName);

			return parsedArg;
		}