当前位置: 首页>>代码示例>>C#>>正文


C# HttpContextBase.ThrowIfNull方法代码示例

本文整理汇总了C#中System.Web.HttpContextBase.ThrowIfNull方法的典型用法代码示例。如果您正苦于以下问题:C# HttpContextBase.ThrowIfNull方法的具体用法?C# HttpContextBase.ThrowIfNull怎么用?C# HttpContextBase.ThrowIfNull使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Web.HttpContextBase的用法示例。


在下文中一共展示了HttpContextBase.ThrowIfNull方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: MapAsync

		public async Task<MapResult> MapAsync(HttpContextBase context, Type type, MethodInfo method, ParameterInfo parameter)
		{
			context.ThrowIfNull("context");
			type.ThrowIfNull("type");
			method.ThrowIfNull("method");
			parameter.ThrowIfNull("parameter");

			Type parameterType = parameter.ParameterType;
			object model = _container != null ? _container.GetInstance(parameterType) : Activator.CreateInstance(parameterType);
			Type modelType = model.GetType();

			foreach (PropertyInfo property in modelType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
			{
				object mappedValue = await GetMappedValueAsync(context, modelType, property);

				if (mappedValue == null)
				{
					throw new ApplicationException(String.Format("Unable to map property '{0} {1}' of type '{2}'.", property.PropertyType.FullName, property.Name, modelType.FullName));
				}

				property.SetValue(model, mappedValue, null);
			}

			return MapResult.ValueMapped(model);
		}
开发者ID:nathan-alden,项目名称:junior-route,代码行数:25,代码来源:ModelMapper.cs

示例2: CanMapTypeAsync

        public Task<bool> CanMapTypeAsync(HttpContextBase context, Type parameterType)
        {
            context.ThrowIfNull("context");
            parameterType.ThrowIfNull("parameterType");

            return true.AsCompletedTask();
        }
开发者ID:nathan-alden,项目名称:junior-route,代码行数:7,代码来源:DefaultValueMapper.cs

示例3: GetResponseAsync

        public Task<ResponseResult> GetResponseAsync(HttpContextBase context, IEnumerable<RouteMatchResult> routeMatchResults)
        {
            context.ThrowIfNull("context");
            routeMatchResults.ThrowIfNull("routeMatchResults");

            return ResponseResult.ResponseGenerated(new Response().NotFound()).AsCompletedTask();
        }
开发者ID:nathan-alden,项目名称:junior-route,代码行数:7,代码来源:NotFoundGenerator.cs

示例4: CanMapTypeAsync

        public override Task<bool> CanMapTypeAsync(HttpContextBase context, Type parameterType)
        {
            context.ThrowIfNull("context");
            parameterType.ThrowIfNull("parameterType");

            return parameterType.ImplementsInterface<IConvertible>().AsCompletedTask();
        }
开发者ID:nathan-alden,项目名称:junior-route,代码行数:7,代码来源:ConvertibleMapper.cs

示例5: CanMapTypeAsync

        public Task<bool> CanMapTypeAsync(HttpContextBase context, Type parameterType)
        {
            context.ThrowIfNull("context");
            parameterType.ThrowIfNull("parameterType");

            return (parameterType == typeof(HttpServerUtilityBase)).AsCompletedTask();
        }
开发者ID:nathan-alden,项目名称:junior-route,代码行数:7,代码来源:HttpServerUtilityBaseMapper.cs

示例6: MapAsync

		public Task<MapResult> MapAsync(HttpContextBase context, Type type, MethodInfo method, ParameterInfo parameter)
		{
			context.ThrowIfNull("request");
			type.ThrowIfNull("type");
			method.ThrowIfNull("method");
			parameter.ThrowIfNull("parameter");

			Type parameterType = parameter.ParameterType;
			var reader = new StreamReader(context.Request.InputStream, context.Request.ContentEncoding);
			string json = reader.ReadToEnd();
			object jsonModel;

			try
			{
				jsonModel = JsonConvert.DeserializeObject(json, parameterType, _serializerSettings);
			}
			catch (Exception exception)
			{
				if (_errorHandling == DataConversionErrorHandling.ThrowException)
				{
					throw new ApplicationException(String.Format("Request content could not be deserialized to '{0}'.", parameterType.FullName), exception);
				}
				jsonModel = parameterType.GetDefaultValue();
			}

			return MapResult.ValueMapped(jsonModel).AsCompletedTask();
		}
开发者ID:nathan-alden,项目名称:junior-route,代码行数:27,代码来源:JsonModelMapper.cs

示例7: CanMapTypeAsync

		public async Task<bool> CanMapTypeAsync(HttpContextBase context, Type parameterType)
		{
			context.ThrowIfNull("context");
			parameterType.ThrowIfNull("parameterType");

			return await Task.Run(() => _parameterTypeMatchDelegate(parameterType));
		}
开发者ID:nathan-alden,项目名称:junior-route,代码行数:7,代码来源:ModelMapper.cs

示例8: CanMapTypeAsync

		public async Task<bool> CanMapTypeAsync(HttpContextBase context, Type parameterType)
		{
			context.ThrowIfNull("context");
			parameterType.ThrowIfNull("parameterType");

			return context.Request.ContentType == "application/json" && await Task.Run(() => _parameterTypeMatchDelegate(parameterType));
		}
开发者ID:nathan-alden,项目名称:junior-route,代码行数:7,代码来源:JsonModelMapper.cs

示例9: OnMapAsync

        protected override Task<MapResult> OnMapAsync(HttpContextBase context, string value, Type parameterType)
        {
            context.ThrowIfNull("context");
            value.ThrowIfNull("value");
            parameterType.ThrowIfNull("parameterType");

            return MapResult.ValueMapped(((IConvertible)value).ToType(parameterType, CultureInfo.InvariantCulture)).AsCompletedTask();
        }
开发者ID:nathan-alden,项目名称:junior-route,代码行数:8,代码来源:ConvertibleMapper.cs

示例10: GetParameterValuesAsync

		public async Task<IEnumerable<object>> GetParameterValuesAsync(HttpContextBase context, Type type, MethodInfo method)
		{
			context.ThrowIfNull("context");
			type.ThrowIfNull("type");
			method.ThrowIfNull("method");

			ParameterInfo[] parameterInfos = method.GetParameters();
			var parameterValues = new List<object>();

			foreach (ParameterInfo parameterInfo in parameterInfos)
			{
				Type parameterType = parameterInfo.ParameterType;
				string parameterName = parameterInfo.Name;
				Type currentParameterType = parameterType;

				do
				{
					bool mapped = false;

					foreach (IParameterMapper parameterMapper in _parameterMappers)
					{
						if (!await parameterMapper.CanMapTypeAsync(context, parameterType))
						{
							continue;
						}
						MapResult mapResult = await parameterMapper.MapAsync(context, type, method, parameterInfo);

						if (mapResult.ResultType == MapResultType.ValueNotMapped)
						{
							continue;
						}

						parameterValues.Add(mapResult.Value);
						mapped = true;
						break;
					}
					if (mapped)
					{
						break;
					}

					currentParameterType = currentParameterType.BaseType;
				} while (currentParameterType != null);

				if (currentParameterType == null)
				{
					throw new ApplicationException(
						String.Format(
							"No request parameter mapper was found for parameter '{0} {1}' of {2}.{3}.",
							parameterType.FullName,
							parameterName,
							type.FullName,
							method.Name));
				}
			}

			return parameterValues;
		}
开发者ID:nathan-alden,项目名称:junior-route,代码行数:58,代码来源:ParameterValueRetriever.cs

示例11: MapAsync

        public Task<MapResult> MapAsync(HttpContextBase context, Type type, MethodInfo method, ParameterInfo parameter)
        {
            context.ThrowIfNull("context");
            type.ThrowIfNull("type");
            method.ThrowIfNull("method");
            parameter.ThrowIfNull("parameter");

            return MapResult.ValueMapped(context.Server).AsCompletedTask();
        }
开发者ID:nathan-alden,项目名称:junior-route,代码行数:9,代码来源:HttpServerUtilityBaseMapper.cs

示例12: HandleResponseAsync

		public async Task<ResponseHandlerResult> HandleResponseAsync(HttpContextBase context, IResponse suggestedResponse, ICache cache, string cacheKey)
		{
			context.ThrowIfNull("context");
			suggestedResponse.ThrowIfNull("suggestedResponse");

			await new CacheResponse(suggestedResponse).WriteResponseAsync(context.Response);

			return ResponseHandlerResult.ResponseWritten();
		}
开发者ID:nathan-alden,项目名称:junior-route,代码行数:9,代码来源:NonCacheableResponseHandler.cs

示例13: GetResponseAsync

        public Task<ResponseResult> GetResponseAsync(HttpContextBase context, IEnumerable<RouteMatchResult> routeMatchResults)
        {
            context.ThrowIfNull("context");
            routeMatchResults.ThrowIfNull("routeMatchResults");

            RouteMatchResult[] unmatchedResults = routeMatchResults.Where(arg => arg.MatchResult.ResultType == MatchResultType.RouteNotMatched).ToArray();

            if (!unmatchedResults.Any())
            {
                return ResponseResult.ResponseNotGenerated().AsCompletedTask();
            }

            RouteMatchResult[] unmatchedResultsThatMatchedOnUrlRelativePath = unmatchedResults.Where(arg1 => RouteMatchedUrlRelativePath(arg1.MatchResult)).ToArray();
            int minimumUnmatchedRestrictions = unmatchedResultsThatMatchedOnUrlRelativePath.Any() ? unmatchedResultsThatMatchedOnUrlRelativePath.Min(arg => arg.MatchResult.UnmatchedRestrictions.Count()) : 0;
            RouteMatchResult[] closestMatches = unmatchedResultsThatMatchedOnUrlRelativePath.Where(arg => arg.MatchResult.UnmatchedRestrictions.Count() == minimumUnmatchedRestrictions).ToArray();

            if (closestMatches.Length != 1)
            {
                return ResponseResult.ResponseNotGenerated().AsCompletedTask();
            }

            RouteMatchResult closestMatch = closestMatches[0];
            IRestriction[] unmatchedRestrictions = closestMatch.MatchResult.UnmatchedRestrictions.ToArray();
            MethodRestriction[] methodRestrictions = unmatchedRestrictions.OfType<MethodRestriction>().ToArray();

            if (methodRestrictions.Any())
            {
                IEnumerable<string> methods = methodRestrictions
                    .Select(arg => arg.Method)
                    .Distinct(StringComparer.OrdinalIgnoreCase)
                    .OrderBy(arg => arg);

                return ResponseResult.ResponseGenerated(new Response().MethodNotAllowed().Header("Allow", String.Join(", ", methods))).AsCompletedTask();
            }
            if (unmatchedRestrictions.OfType<HeaderRestriction<AcceptHeader>>().Any())
            {
                return ResponseResult.ResponseGenerated(new Response().NotAcceptable()).AsCompletedTask();
            }
            if (unmatchedRestrictions.OfType<HeaderRestriction<AcceptCharsetHeader>>().Any())
            {
                return ResponseResult.ResponseGenerated(new Response().NotAcceptable()).AsCompletedTask();
            }
            if (unmatchedRestrictions.OfType<HeaderRestriction<AcceptEncodingHeader>>().Any())
            {
                return ResponseResult.ResponseGenerated(new Response().NotAcceptable()).AsCompletedTask();
            }
            if (unmatchedRestrictions.OfType<HeaderRestriction<ContentEncodingHeader>>().Any())
            {
                return ResponseResult.ResponseGenerated(new Response().UnsupportedMediaType()).AsCompletedTask();
            }

            return ResponseResult.ResponseNotGenerated().AsCompletedTask();
        }
开发者ID:nathan-alden,项目名称:junior-route,代码行数:53,代码来源:UnmatchedRestrictionsGenerator.cs

示例14: MapAsync

        public Task<MapResult> MapAsync(HttpContextBase context, Type modelType, PropertyInfo property)
        {
            context.ThrowIfNull("context");
            modelType.ThrowIfNull("modelType");
            property.ThrowIfNull("property");

            Type propertyType = property.PropertyType;
            string propertyName = property.Name;
            NameValueCollection nameValueCollection;

            switch (_source)
            {
                case NameValueCollectionSource.Form:
                    nameValueCollection = context.Request.Form;
                    break;
                case NameValueCollectionSource.QueryString:
                    nameValueCollection = context.Request.QueryString;
                    break;
                default:
                    throw new InvalidOperationException(String.Format("Unexpected name-value collection source {0}.", _source));
            }
            string field = nameValueCollection.AllKeys.LastOrDefault(arg => String.Equals(arg, propertyName, _caseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase));

            if (field == null)
            {
                return MapResult.ValueNotMapped().AsCompletedTask();
            }

            string value = nameValueCollection[field];

            try
            {
                return OnMapAsync(context, value, propertyType);
            }
            catch (Exception exception)
            {
                if (_errorHandling == DataConversionErrorHandling.ThrowException)
                {
                    throw new ApplicationException(
                        String.Format(
                            "Value of form field '{0}' could not be converted to property '{1} {2}' of type '{3}'.",
                            field,
                            propertyType.FullName,
                            propertyName,
                            modelType.FullName),
                        exception);
                }

                return MapResult.ValueMapped(propertyType.GetDefaultValue()).AsCompletedTask();
            }
        }
开发者ID:nathan-alden,项目名称:junior-route,代码行数:51,代码来源:RequestMapper.cs

示例15: MapAsync

		public Task<MapResult> MapAsync(HttpContextBase context, Type type, MethodInfo method, ParameterInfo parameter)
		{
			context.ThrowIfNull("context");
			type.ThrowIfNull("type");
			method.ThrowIfNull("method");
			parameter.ThrowIfNull("parameter");

			Type parameterType = parameter.ParameterType;
			string parameterName = parameter.Name;
			string field = context.Request.Form.AllKeys.LastOrDefault(arg => String.Equals(arg, parameterName, _caseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase));

			if (field == null)
			{
				return MapResult.ValueNotMapped().AsCompletedTask();
			}

			IConvertible value = context.Request.Form[field];
			object convertedValue;

			try
			{
				convertedValue = value.ToType(parameterType, CultureInfo.InvariantCulture);
			}
			catch (Exception exception)
			{
				if (_errorHandling == DataConversionErrorHandling.ThrowException)
				{
					throw new ApplicationException(
						String.Format(
							"Value for form field '{0}' could not be converted to parameter '{1} {2}' of {3}.{4}.",
							field,
							parameterType.FullName,
							parameterName,
							type.FullName,
							method.Name),
						exception);
				}
				convertedValue = parameterType.GetDefaultValue();
			}

			return MapResult.ValueMapped(convertedValue).AsCompletedTask();
		}
开发者ID:kelong,项目名称:JuniorRoute,代码行数:42,代码来源:FormToIConvertibleMapper.cs


注:本文中的System.Web.HttpContextBase.ThrowIfNull方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。