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


C# HttpError.Add方法代码示例

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


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

示例1: CreateErrorResponse

 internal static HttpResponseMessage CreateErrorResponse(this HttpRequestMessage request, HttpStatusCode statusCode, string message, string messageDetail)
 {
     HttpError error = new HttpError(message);
     HttpConfiguration config = request.GetConfiguration();
     if (config != null && ShouldIncludeErrorDetail(config, request))
     {
         error.Add(MessageDetailKey, messageDetail);
     }
     return request.CreateErrorResponse(statusCode, error);
 }
开发者ID:Swethach,项目名称:aspnetwebstack,代码行数:10,代码来源:HttpRequestMessageExtensions.cs

示例2: ConverToHttpResponse

        public static HttpResponseMessage ConverToHttpResponse(HttpActionExecutedContext context)
        {

            var exception = context.Exception;
            var error = new HttpError();
            var dic = ExceptionConverterService.Convert(exception);
            foreach (var item in dic)
            {
                error.Add(item.Key, item.Value);
            }
            return context.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, error);
        }
开发者ID:ehsmohammadi,项目名称:BTE.RMS,代码行数:12,代码来源:WebApiExceptionAdapter.cs

示例3: OnActionExecuting

        /// <summary>
        /// Occurs before the action method is invoked.
        /// </summary>
        /// <param name="actionContext">The action context.</param>
        public override void OnActionExecuting( HttpActionContext actionContext )
        {
            ModelStateDictionary modelState = actionContext.ModelState;

            IEntity valueArg = null;
            if ( actionContext.ActionArguments.ContainsKey( "value" ) )
            {
                valueArg = actionContext.ActionArguments["value"] as IEntity;
            }

            if ( valueArg != null )
            {
                Type entityType = actionContext.ActionArguments["value"].GetType();
                IgnoreModelErrorsAttribute ignoreModelErrorsAttribute = entityType.GetCustomAttributes( typeof( IgnoreModelErrorsAttribute ), true ).FirstOrDefault() as IgnoreModelErrorsAttribute;

                if ( ignoreModelErrorsAttribute != null )
                {
                    foreach ( string key in ignoreModelErrorsAttribute.Keys )
                    {
                        IEnumerable<string> matchingKeys = modelState.Keys.Where( x => Regex.IsMatch( x, key ) );
                        foreach ( string matchingKey in matchingKeys )
                        {
                            modelState[matchingKey].Errors.Clear();
                        }
                    }
                }
            }

            if ( !actionContext.ModelState.IsValid )
            {
                HttpError httpError = new HttpError();

                foreach ( var item in actionContext.ModelState )
                {
                    foreach ( ModelError error in item.Value.Errors )
                    {
                        if ( !httpError.ContainsKey( item.Key ) )
                        {
                            httpError.Add( item.Key, string.Empty );
                            httpError[item.Key] += error.ErrorMessage;
                        }
                        else
                        {
                            httpError[item.Key] += Environment.NewLine + error.ErrorMessage;
                        }
                    }
                }

                actionContext.Response = actionContext.Request.CreateErrorResponse( HttpStatusCode.BadRequest, httpError );
            }
        }
开发者ID:Ganon11,项目名称:Rock,代码行数:55,代码来源:ValidateAttribute.cs

示例4: Handle

 public override void Handle(ExceptionHandlerContext context)
 {
     const string Message = "Your input conflicted with the current state of the system.";
     if (context.Exception is DivideByZeroException)
     {
         context.Result = new ResponseMessageResult(context.Request.CreateErrorResponse(HttpStatusCode.Conflict, Message));
     }
     else
     {
         HttpError error = new HttpError("An interval server error occured.");
         error.Add("CorrelationId", context.Request.GetCorrelationId().ToString());
         var response = context.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, error);
         context.Result = new ResponseMessageResult(response);
     }
 }
开发者ID:mahf,项目名称:ASPNETWebAPISamples,代码行数:15,代码来源:WebApiConfig.cs

示例5: CreateErrorResponse

        private HttpResponseMessage CreateErrorResponse(HttpRequestMessage request, Exception ex, HttpStatusCode statusCode) {
            HttpConfiguration configuration = request.GetConfiguration();
            HttpError error = new HttpError(ex, request.ShouldIncludeErrorDetail());

            string lastId = _coreLastReferenceIdManager.GetLastReferenceId();
            if (!String.IsNullOrEmpty(lastId))
                error.Add("Reference", lastId);

            // CreateErrorResponse should never fail, even if there is no configuration associated with the request
            // In that case, use the default HttpConfiguration to con-neg the response media type
            if (configuration == null) {
                using (HttpConfiguration defaultConfig = new HttpConfiguration()) {
                    return request.CreateResponse(statusCode, error, defaultConfig);
                }
            }

            return request.CreateResponse(statusCode, error, configuration);
        }
开发者ID:aamarber,项目名称:Exceptionless,代码行数:18,代码来源:ExceptionlessReferenceIdExceptionHandler.cs

示例6: Execute

        private HttpResponseMessage Execute()
        {
            var httpResponseMessage = new HttpResponseMessage();

            try
            {
                var negotiationResult = ContentNegotiator.Negotiate(typeof(HttpError), Request, Formatters);

                if (negotiationResult == null)
                {
                    httpResponseMessage.StatusCode = HttpStatusCode.NotAcceptable;
                }
                else
                {
                    var error = new HttpError("Internal Server Error");
                    foreach (var property in Exception.GetCustomProperties())
                    {
                        error.Add(property.Key, property.Value);
                    }

                    httpResponseMessage.StatusCode = HttpStatusCode.InternalServerError;
                    httpResponseMessage.Content = new ObjectContent<HttpError>(error, negotiationResult.Formatter, negotiationResult.MediaType);
                }

                httpResponseMessage.RequestMessage = Request;
            }
            catch
            {
                httpResponseMessage.Dispose();
                throw;
            }

            return httpResponseMessage;
        }
开发者ID:magonzalez,项目名称:WebApi-OAuth2-StarterKit,代码行数:34,代码来源:ReferencedExceptionResult.cs

示例7: CreateErrorResponse

 internal static HttpResponseMessage CreateErrorResponse(this HttpRequestMessage request, HttpStatusCode statusCode, string message, string messageDetail)
 {
     HttpError error = new HttpError(message);
     if (request.ShouldIncludeErrorDetail())
     {
         error.Add(MessageDetailKey, messageDetail);
     }
     return request.CreateErrorResponse(statusCode, error);
 }
开发者ID:balajivasudevan,项目名称:aspnetwebstack,代码行数:9,代码来源:ODataHttpRequestMessageExtensions.cs

示例8: OnActionExecuting

        /// <summary>
        /// Occurs before the action method is invoked.
        /// </summary>
        /// <param name="actionContext">The action context.</param>
        public override void OnActionExecuting( HttpActionContext actionContext )
        {
            ModelStateDictionary modelState = actionContext.ModelState;

            //// Remove any model errors that should be ignored based on IgnoreModelErrorsAttribute
            // determine the entity parameter so we can clear the model errors on the ignored properties
            IEntity valueArg = null;
            if ( actionContext.ActionArguments.Count > 0 )
            {
                // look a parameter with the name 'value', if not found, get the first parameter that is an IEntity
                if ( actionContext.ActionArguments.ContainsKey( "value" ) )
                {
                    valueArg = actionContext.ActionArguments["value"] as IEntity;
                }
                else
                {
                    valueArg = actionContext.ActionArguments.Select( a => a.Value ).Where( a => a is IEntity ).FirstOrDefault() as IEntity;
                }
            }

            // if we found the entityParam, clear the model errors on ignored properties
            if ( valueArg != null )
            {
                Type entityType = valueArg.GetType();
                IgnoreModelErrorsAttribute ignoreModelErrorsAttribute = entityType.GetCustomAttributes( typeof( IgnoreModelErrorsAttribute ), true ).FirstOrDefault() as IgnoreModelErrorsAttribute;

                if ( ignoreModelErrorsAttribute != null )
                {
                    foreach ( string key in ignoreModelErrorsAttribute.Keys )
                    {
                        IEnumerable<string> matchingKeys = modelState.Keys.Where( x => Regex.IsMatch( x, key ) );
                        foreach ( string matchingKey in matchingKeys )
                        {
                            modelState[matchingKey].Errors.Clear();
                        }
                    }
                }
            }

            // now that the IgnoreModelErrorsAttribute properties have been cleared, deal with the remaining model state validations
            if ( !actionContext.ModelState.IsValid )
            {
                HttpError httpError = new HttpError();

                foreach ( var item in actionContext.ModelState )
                {
                    var msg = new System.Text.StringBuilder();

                    foreach ( ModelError error in item.Value.Errors )
                    {
                        if ( !string.IsNullOrWhiteSpace( error.ErrorMessage ) )
                        {
                            msg.Append( msg.Length > 0 ? "; " : "" );
                            msg.Append( error.ErrorMessage );
                        }

                        if ( error.Exception != null && !string.IsNullOrWhiteSpace( error.Exception.Message ) )
                        {
                            msg.Append( msg.Length > 0 ? "; " : "" );
                            msg.Append( error.Exception.Message );
                        }
                    }

                    httpError.Add( item.Key, msg.ToString() );
                }

                actionContext.Response = actionContext.Request.CreateErrorResponse( HttpStatusCode.BadRequest, httpError );
            }
        }
开发者ID:NewSpring,项目名称:Rock,代码行数:73,代码来源:ValidateAttribute.cs

示例9: Execute

        private HttpResponseMessage Execute()
        {
            var httpResponseMessage = new HttpResponseMessage();

            try
            {
                var negotiationResult = ContentNegotiator.Negotiate(typeof(HttpError), Request, Formatters);

                if (negotiationResult == null)
                {
                    httpResponseMessage.StatusCode = HttpStatusCode.NotAcceptable;
                }
                else
                {
                    var error = new HttpError("Validation Failed");
                    foreach (var err in _exception.ValidationErrors)
                    {
                        if (!error.ContainsKey(err.ItemName))
                        {
                            error.Add(err.ItemName, new Collection<ApiError>());
                        }

                        ((ICollection<ApiError>)error[err.ItemName]).Add(new ApiError
                        {
                            ErrorCode = err.ErrorCode,
                            Message = err.ErrorMessage
                        });
                    }

                    httpResponseMessage.StatusCode = HttpStatusCode.BadRequest;
                    httpResponseMessage.Content = new ObjectContent<HttpError>(error, negotiationResult.Formatter, negotiationResult.MediaType);
                }

                httpResponseMessage.RequestMessage = Request;
            }
            catch
            {
                httpResponseMessage.Dispose();
                throw;
            }

            return httpResponseMessage;
        }
开发者ID:magonzalez,项目名称:WebApi-OAuth2-StarterKit,代码行数:43,代码来源:ValidationExceptionResult.cs

示例10: OnActionExecuting

        /// <summary>
        /// Occurs before the action method is invoked.
        /// </summary>
        /// <param name="actionContext">The action context.</param>
        public override void OnActionExecuting( HttpActionContext actionContext )
        {
            ModelStateDictionary modelState = actionContext.ModelState;

            IEntity valueArg = null;
            if ( actionContext.ActionArguments.ContainsKey( "value" ) )
            {
                valueArg = actionContext.ActionArguments["value"] as IEntity;
            }

            if ( valueArg != null )
            {
                Type entityType = actionContext.ActionArguments["value"].GetType();
                IgnoreModelErrorsAttribute ignoreModelErrorsAttribute = entityType.GetCustomAttributes( typeof( IgnoreModelErrorsAttribute ), true ).FirstOrDefault() as IgnoreModelErrorsAttribute;

                if ( ignoreModelErrorsAttribute != null )
                {
                    foreach ( string key in ignoreModelErrorsAttribute.Keys )
                    {
                        IEnumerable<string> matchingKeys = modelState.Keys.Where( x => Regex.IsMatch( x, key ) );
                        foreach ( string matchingKey in matchingKeys )
                        {
                            modelState[matchingKey].Errors.Clear();
                        }
                    }
                }
            }

            if ( !actionContext.ModelState.IsValid )
            {
                HttpError httpError = new HttpError();

                foreach ( var item in actionContext.ModelState )
                {
                    var msg = new System.Text.StringBuilder();

                    foreach ( ModelError error in item.Value.Errors )
                    {
                        if ( !string.IsNullOrWhiteSpace( error.ErrorMessage ) )
                        {
                            msg.Append( msg.Length > 0 ? "; " : "" );
                            msg.Append( error.ErrorMessage );
                        }

                        if ( error.Exception != null && !string.IsNullOrWhiteSpace( error.Exception.Message ) )
                        {
                            msg.Append( msg.Length > 0 ? "; " : "" );
                            msg.Append( error.Exception.Message );
                        }
                    }

                    httpError.Add( item.Key, msg.ToString() );
                }

                actionContext.Response = actionContext.Request.CreateErrorResponse( HttpStatusCode.BadRequest, httpError );
            }
        }
开发者ID:tcavaletto,项目名称:Rock-CentralAZ,代码行数:61,代码来源:ValidateAttribute.cs


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