本文整理汇总了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);
}
示例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);
}
示例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 );
}
}
示例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);
}
}
示例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);
}
示例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;
}
示例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);
}
示例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 );
}
}
示例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;
}
示例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 );
}
}