本文整理汇总了C#中System.Web.Http.Filters.HttpActionExecutedContext类的典型用法代码示例。如果您正苦于以下问题:C# HttpActionExecutedContext类的具体用法?C# HttpActionExecutedContext怎么用?C# HttpActionExecutedContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
HttpActionExecutedContext类属于System.Web.Http.Filters命名空间,在下文中一共展示了HttpActionExecutedContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: OnException
public override void OnException(HttpActionExecutedContext actionExecutedContext)
{
if (actionExecutedContext.Exception != null)
{
var exception = actionExecutedContext.Exception;
var actionName = actionExecutedContext.ActionContext.ActionDescriptor.ActionName;
var controllerName = actionExecutedContext.ActionContext.ControllerContext.ControllerDescriptor.ControllerName;
string resourceFile = String.IsNullOrEmpty(LocalResourceFile)
? Localization.ExceptionsResourceFile
: LocalResourceFile;
string key = String.IsNullOrEmpty(MessageKey) ? controllerName + "_" + actionName + ".Error" : MessageKey;
var response = new HttpResponseMessage
{
StatusCode = HttpStatusCode.InternalServerError,
ReasonPhrase = Localization.GetString(key, resourceFile)
};
actionExecutedContext.Response = response;
Exceptions.LogException(exception);
}
}
示例2: OnException
public override void OnException(HttpActionExecutedContext context)
{
if (context.Exception is ValidationException)
{
RespondWithBadRequest(context);
}
}
示例3: OnException
public override void OnException(HttpActionExecutedContext context)
{
if (MvcApplication.Logger != null && context.Exception != null)
{
MvcApplication.Logger.Error("ExceptionHandlingAttribute", context.Exception);
}
if (context.Exception is System.Data.DataException)
{
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Content = new StringContent(context.Exception.Message),
ReasonPhrase = "Exception"
});
}
//Log Critical errors
Debug.WriteLine(context.Exception);
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Content = new StringContent("An error occurred, please try again or contact the administrator."),
ReasonPhrase = "Critical Exception"
});
}
示例4: OnActionExecuted
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
var buffer = TraceBuffer.Current;
if (buffer == null)
{
return;
}
TraceBuffer.Clear();
if (buffer.HasContent)
{
string originalContent;
HttpStatusCode statusCode;
if (actionExecutedContext.Exception == null)
{
originalContent = actionExecutedContext.Response.Content.ReadAsStringAsync().Result;
statusCode = actionExecutedContext.Response.StatusCode;
}
else
{
originalContent = actionExecutedContext.Exception.ToLogString();
statusCode = HttpStatusCode.InternalServerError;
}
actionExecutedContext.Response = new HttpResponseMessage(statusCode)
{
Content = new StringContent(string.Format("{0}\n\n{1}", buffer, originalContent).Trim())
};
}
}
示例5: End
private void End(HttpActionExecutedContext actionExecutedContext)
{
if (actionExecutedContext.Exception != null)
WindsorAccessor.Instance.Container.Resolve<IUnitOfWork>().Abort();
WindsorAccessor.Instance.Container.Resolve<IUnitOfWork>().End();
}
示例6: OnActionExecuted
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
if (actionExecutedContext.Response != null)
actionExecutedContext.Response.Headers.Add("Access-Control-Allow-Origin", "*");
base.OnActionExecuted(actionExecutedContext);
}
示例7: OnActionExecuted
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
try
{
//Apply logging
var objChanges = HttpContext.Current.Items[ContactInfo.CONTACT_CHANGE] as List<ActivityObjectChangeInfo>;
if (objChanges != null)
{
var activityInfo = new ActivityLogInfo
{
FunctionId = 0,
CreatedBy = UserContext.GetCurrentUser().UserID
};
activityInfo.Id = ActivityLogRepository.Create(activityInfo);
foreach (var objChange in objChanges)
{
objChange.ActivityId = activityInfo.Id;
ActivityObjectChangeRepository.Create(objChange);
}
}
HttpContext.Current.Items[ContactInfo.CONTACT_CHANGE] = null;
}
catch
{
//Dont throw exception if loging failed
}
base.OnActionExecuted(actionExecutedContext);
}
示例8: OnException
public override void OnException(HttpActionExecutedContext actionExecutedContext)
{
if (actionExecutedContext.Exception is DraftAuthenticationException)
{
actionExecutedContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized)
{
Content = new StringContent("Unauthorized"),
ReasonPhrase = "Unauthorized"
};
}
else if (actionExecutedContext.Exception is HttpStatusException)
{
HttpStatusException exc = (HttpStatusException)actionExecutedContext.Exception;
actionExecutedContext.Response = new HttpResponseMessage(exc.Status)
{
Content = new StringContent(exc.Message),
ReasonPhrase = exc.Message
};
}
else if (actionExecutedContext.Exception != null)
{
Exception exc = actionExecutedContext.Exception;
actionExecutedContext.Response = new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Content = new StringContent(exc.Message),
ReasonPhrase = exc.Source + " : " + exc.GetType().ToString() + " : " + exc.Message
};
}
}
示例9: OnException
public override void OnException(HttpActionExecutedContext actionExecutedContext)
{
if (actionExecutedContext.Exception is BadRequestException)
{
actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(HttpStatusCode.BadRequest, new HttpError(actionExecutedContext.Exception.Message));
}
}
示例10: OnActionExecuted
/// <summary>
/// Called when the action is executed.
/// </summary>
/// <param name="actionExecutedContext">The action executed context.</param>
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
base.OnActionExecuted(actionExecutedContext);
if (!actionExecutedContext.Response.IsSuccessStatusCode) {
return;
}
object responseObject;
if (!actionExecutedContext.Response.TryGetContentValue(out responseObject)) {
return;
}
var queryable = ApplySelectAndExpand(responseObject as IQueryable, actionExecutedContext.Request);
if (queryable != null) {
// if a select or expand was encountered we need to
// execute the DbQueries here, so that any exceptions thrown can be properly returned.
// if we wait to have the query executed within the serializer, some exceptions will not
// serialize properly.
var rQuery = Enumerable.ToList((dynamic) queryable);
var formatter = ((dynamic) actionExecutedContext.Response.Content).Formatter;
var oc = new ObjectContent(rQuery.GetType(), rQuery, formatter);
actionExecutedContext.Response.Content = oc;
}
Object tmp;
actionExecutedContext.Request.Properties.TryGetValue("MS_InlineCount", out tmp);
var inlineCount = (Int64?) tmp;
if (inlineCount.HasValue) {
actionExecutedContext.Response.Headers.Add("X-InlineCount", inlineCount.ToString());
}
}
示例11: OnException
public override void OnException(HttpActionExecutedContext context)
{
HttpStatusCode status = HttpStatusCode.InternalServerError;
var exType = context.Exception.GetType();
if (exType == typeof(UnauthorizedAccessException))
status = HttpStatusCode.Unauthorized;
else if (exType == typeof(ArgumentException))
status = HttpStatusCode.NotFound;
else if (exType == typeof(ServiceUnavailableExceptions))
status = HttpStatusCode.ServiceUnavailable;
else if (exType == typeof(InvalidOperationException)) // Happens when someone tries to Update on POST instead of PUT.
status = HttpStatusCode.MethodNotAllowed;
else if (exType == typeof(ItemNotFoundException))
status = HttpStatusCode.NotFound;
var apiError = new ApiMessageError() { message = context.Exception.Message };
// create a new response and attach our ApiError object
// which now gets returned on ANY exception result
var errorResponse = context.Request.CreateResponse<ApiMessageError>(status, apiError);
context.Response = errorResponse;
errorResponse.ReasonPhrase = context.Exception.Message;
base.OnException(context);
}
示例12: OnException
public override void OnException(HttpActionExecutedContext context)
{
if (context.Exception is ArgumentNullException)
{
context.Response = context.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "ArgumentNullException occurred");
}
}
示例13: OnActionExecuted
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
if (actionExecutedContext.Response != null)
actionExecutedContext.Response.Headers.Add("Access-Control-Allow-Origin", ConfigurationManager.AppSettings["ClientUrl"]);
base.OnActionExecuted(actionExecutedContext);
}
示例14: OnException
/// <summary>
/// Raises the exception event.
/// </summary>
/// <param name="context">The context for the action.</param>
public override void OnException(HttpActionExecutedContext context)
{
var wrapResultAttribute = HttpActionDescriptorHelper
.GetWrapResultAttributeOrNull(context.ActionContext.ActionDescriptor) ??
_configuration.DefaultWrapResultAttribute;
if (wrapResultAttribute.LogError)
{
LogHelper.LogException(Logger, context.Exception);
}
if (!wrapResultAttribute.WrapOnError)
{
return;
}
if (IsIgnoredUrl(context.Request.RequestUri))
{
return;
}
context.Response = context.Request.CreateResponse(
GetStatusCode(context),
new AjaxResponse(
SingletonDependency<ErrorInfoBuilder>.Instance.BuildForException(context.Exception),
context.Exception is Abp.Authorization.AbpAuthorizationException)
);
EventBus.Trigger(this, new AbpHandledExceptionData(context.Exception));
}
示例15: GetContactLink
Uri GetContactLink(int contactId, HttpActionExecutedContext context)
{
var routeData = context.Request.GetRouteData();
var controller = routeData.Values["controller"];
var urlHelper = context.Request.GetUrlHelper();
return new Uri(urlHelper.Route("DefaultApi", new { controller = controller, id = contactId }), UriKind.Relative);
}