/// <summary> /// Get a named parameter. /// </summary> /// <remarks> /// Will throw <see cref="DreamAbortException"/> if the named parameter does not exist. /// </remarks> /// <param name="key"><see cref="DreamFeatureParamAttribute"/> name.</param> /// <returns>Text value of parameter.</returns> /// <exception cref="DreamAbortException">Throws if parameter does not exist.</exception> public string GetParam(string key) { EnsureFeatureIsSet(); if (key == null) { throw new ArgumentNullException("key"); } string result; string[] values; _pathParams.TryGetValue(key, out values); if ((values != null) && (values.Length > 0)) { result = values[0]; } else { result = Uri.GetParam(key, null); } if (result == null) { throw new DreamAbortException(DreamMessage.BadRequest(string.Format("missing feature parameter '{0}'", key))); } return(result); }
/// <summary> /// Get a named parameter. /// </summary> /// <typeparam name="T">Type to convert parameter to.</typeparam> /// <param name="key"><see cref="DreamFeatureParamAttribute"/> name.</param> /// <returns>Parameter value converted to requested type.</returns> public T GetParam <T>(string key) { string result = GetParam(key); try { return((T)SysUtil.ChangeType(result, typeof(T))); } catch { throw new DreamAbortException(DreamMessage.BadRequest(string.Format("invalid value for feature parameter '{0}'", key))); } }
/// <summary> /// Get a named parameter. /// </summary> /// <typeparam name="T">Type to convert parameter to.</typeparam> /// <param name="key"><see cref="DreamFeatureParamAttribute"/> name.</param> /// <param name="def">Default value to return in case parameter is not defined.</param> /// <returns>Parameter value converted to requested type.</returns> public T GetParam <T>(string key, T def) where T : struct { string result = GetParam(key, null); if (result != null) { try { return((T)SysUtil.ChangeType <T>(result)); } catch { throw new DreamAbortException(DreamMessage.BadRequest(string.Format("invalid value for feature parameter '{0}'", key))); } } return(def); }
private static DreamFeatureAdapter MakeConvertingContextParamGetter(string name, Type type) { return(new DreamFeatureAdapter(name, (context, request, response) => { object value = context.GetParam(name, null); if (value == null && type.IsInterface) { object resolvedInstance; if (context.Container.TryResolve(type, out resolvedInstance)) { return resolvedInstance; } } if (value != null) { return SysUtil.ChangeType(value, type); } if (!type.IsValueType || (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))) { return null; } throw new DreamAbortException(DreamMessage.BadRequest(string.Format("invalid value for feature parameter '{0}'", name))); })); }
/// <summary> /// Create a new instance for a <see cref="DreamStatus.BadRequest"/> condition. /// </summary> /// <param name="message">Text message to use for <see cref="Exception.Message"/> and the internal <see cref="DreamMessage"/>.</param> /// <param name="innerException">Inner Exception</param> public DreamBadRequestException(string message, Exception innerException) : base(DreamMessage.BadRequest(message), message, innerException) { }
//--- Constructors --- /// <summary> /// Create a new instance for a <see cref="DreamStatus.BadRequest"/> condition. /// </summary> /// <param name="message">Text message to use for <see cref="Exception.Message"/> and the internal <see cref="DreamMessage"/>.</param> public DreamBadRequestException(string message) : base(DreamMessage.BadRequest(message), message) { }