本文整理汇总了C#中ActionExecutedContext类的典型用法代码示例。如果您正苦于以下问题:C# ActionExecutedContext类的具体用法?C# ActionExecutedContext怎么用?C# ActionExecutedContext使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ActionExecutedContext类属于命名空间,在下文中一共展示了ActionExecutedContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: OnActionExecuted
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
ControllerBase controller = filterContext.Controller as ControllerBase;
if (controller != null)
{
string serializedModelState = controller.TempData[Key] as string;
if (!string.IsNullOrEmpty(serializedModelState))
{
IEnumerable<ModelStateWrapper> modelStateWrappers = JsonConvert.DeserializeObject<IEnumerable<ModelStateWrapper>>(serializedModelState, new JsonSerializerSettings() { Error = DeserializationErrorHandler });
if (modelStateWrappers != null)
{
ModelStateDictionary modelState = new ModelStateDictionary();
foreach (ModelStateWrapper modelStateWrapper in modelStateWrappers)
{
ModelStateEntry ms = new ModelStateEntry();
ms.ValidationState = modelStateWrapper.ValidationState;
ms.AttemptedValue = modelStateWrapper.Value;
modelState.Add(modelStateWrapper.Key, ms);
}
if (filterContext.Result is ViewResult)
controller.ViewData.ModelState.Merge(modelState);
else controller.TempData.Remove(Key);
}
}
}
base.OnActionExecuted(filterContext);
}
示例2: OnActionExecuted
public override void OnActionExecuted(ActionExecutedContext context)
{
base.OnActionExecuted(context);
if (context.Exception == null)
{
return;
}
var dbException = context.Exception as DbException;
if (dbException != null)
{
context.Result = new BadRequestObjectResult(dbException);
}
else
{
context.Result = new InternalServerErrorResult();
}
var apiKey = this.config["RaygunSettings:ApiKey"];
if (string.IsNullOrEmpty(apiKey))
{
return;
}
//TODO: update this once Raygun supports ASPNET CORE RTM
//var settings = new RaygunSettings { ApiKey = apiKey };
//var client = new RaygunClient(settings);
//client.Send(context.Exception);
}
示例3: OnActionExecuted
public override void OnActionExecuted(ActionExecutedContext context) {
var exception = context.Exception as CommandRejectedException;
if (exception == null) return;
context.Result = new BadRequestObjectResult(exception.Errors);
context.ExceptionHandled = true;
}
示例4: OnActionExecuted
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
var controller = filterContext.Controller as Controller;
var tempModelState = controller.TempData[Key] as string;
if (tempModelState == null)
{
return;
}
Dictionary<string, List<string>> modelState = (Dictionary<string, List<string>>)JsonConvert.DeserializeObject(
tempModelState, typeof(Dictionary<string, List<string>>));
if (modelState.Keys.Count > 0)
{
//Only Import if we are viewing
if (filterContext.Result is ViewResult)
{
foreach (var entry in modelState)
{
foreach (var error in entry.Value)
{
controller.ViewData.ModelState.AddModelError(entry.Key, error);
}
}
}
else
{
//Otherwise remove it.
controller.TempData.Remove(Key);
}
}
base.OnActionExecuted(filterContext);
}
示例5: OnActionExecuted
/// <summary>
/// アクションが実行されたとに呼び出されます。
/// </summary>
/// <param name="context">実行コンテキスト</param>
public override void OnActionExecuted(ActionExecutedContext context)
{
base.OnActionExecuted(context);
//--- 例外が飛んだ場合は握り潰してエラーを通知
if (context.Exception != null)
{
var color = "#C00000";
context.Result = this.Json(new Message
{
Text = "Unhandled exception occured...",
IsEphemeral = true,
Attachments = new []
{
new Attachment
{
Color = color,
AuthorName = "Type",
AuthorLink = Uri.EscapeUriString($"https://www.google.com/search?q={context.Exception.GetType()}"),
Text = context.Exception.GetType().ToString(),
},
new Attachment
{
Color = color,
AuthorName = nameof(context.Exception.Message),
Text = context.Exception.Message,
},
new Attachment
{
Color = color,
AuthorName = nameof(context.Exception.StackTrace),
Text = context.Exception.StackTrace,
},
new Attachment
{
Color = color,
AuthorName = nameof(context.Exception.InnerException),
Text = context.Exception.InnerException?.Message,
},
}
});
context.ExceptionHandled = true;
}
//--- JSON を返していれば正常終了とみなす
if (context.Result is JsonResult)
return;
//--- ステータスコードを持つ場合はそれを通知
var message = "Error occured...";
var result = context.Result as HttpStatusCodeResult;
if (result != null)
message += Environment.NewLine + $"[HTTP Status Code : {result.StatusCode}]";
context.Result = this.Json(new Message
{
Text = message,
IsEphemeral = true,
});
}
示例6: OnActionExecuted
public override void OnActionExecuted(ActionExecutedContext context)
{
if (context.Result == null || context.Result is NoContentResult)
{
return;
}
if (BadActionResultTransformer.IsBadAction(context.Result))
{
var transformed = BadActionResultTransformer.Transform(context.Result);
context.Result = new ObjectResult(transformed)
{
StatusCode = transformed.Errors.First().Status
};
return;
}
var responseResult = (ObjectResult)context.Result;
var relationshipPaths = FindRelationshipPathsToInclude(context.HttpContext.Request);
if (!configuration.ValidateIncludedRelationshipPaths(relationshipPaths, responseResult.Value))
{
context.Result = new HttpStatusCodeResult(400);
return;
}
var jsonApiContext = new Context(
new Uri(context.HttpContext.Request.GetDisplayUrl()),
relationshipPaths);
responseResult.Value = jsonApiTransformer.Transform(responseResult.Value, jsonApiContext);
}
示例7: OnActionExecuted
public override void OnActionExecuted(ActionExecutedContext context) {
base.OnActionExecuted(context);
var controller = context.Controller as Controller;
if (controller != null) {
controller.ViewData[ParsleyValidationAttributesGenerator.USE_PARSLEY_KEY] = true;
}
}
示例8: OnActionExecuted
public void OnActionExecuted(ActionExecutedContext context)
{
var time = _timer.ElapsedMilliseconds;
context.HttpContext.Response.Headers.Add(
"ActionElapsedTime",
new string[] { time.ToString(CultureInfo.InvariantCulture) + " ms" });
}
示例9: OnActionExecuted
/// <inheritdoc />
public void OnActionExecuted(ActionExecutedContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
var httpResponseException = context.Exception as HttpResponseException;
if (httpResponseException != null)
{
var request = context.HttpContext.GetHttpRequestMessage();
var response = httpResponseException.Response;
if (response != null && response.RequestMessage == null)
{
response.RequestMessage = request;
}
var objectResult = new ObjectResult(response)
{
DeclaredType = typeof(HttpResponseMessage)
};
context.Result = objectResult;
// Its marked as handled as in webapi because an HttpResponseException
// was considered as a 'success' response.
context.ExceptionHandled = true;
}
}
示例10: OnActionExecuted_HandlesExceptionAndReturnsObjectResult
public void OnActionExecuted_HandlesExceptionAndReturnsObjectResult()
{
// Arrange
var filter = new HttpResponseExceptionActionFilter();
var httpContext = new DefaultHttpContext();
httpContext.Request.Method = "GET";
var actionContext = new ActionContext(
httpContext,
new RouteData(),
Mock.Of<ActionDescriptor>());
var context = new ActionExecutedContext(
actionContext,
filters: new List<IFilter>(),
controller: new object());
context.Exception = new HttpResponseException(HttpStatusCode.BadRequest);
// Act
filter.OnActionExecuted(context);
// Assert
Assert.True(context.ExceptionHandled);
var result = Assert.IsType<ObjectResult>(context.Result);
Assert.Equal(typeof(HttpResponseMessage), result.DeclaredType);
var response = Assert.IsType<HttpResponseMessage>(result.Value);
Assert.NotNull(response.RequestMessage);
Assert.Equal(context.HttpContext.GetHttpRequestMessage(), response.RequestMessage);
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
}
示例11: OnActionExecuted
public void OnActionExecuted(ActionExecutedContext context)
{
if (context.ActionDescriptor.DisplayName == "FiltersSample.Controllers.HomeController.Hello")
{
// Manipulating action result...
context.Result = Helpers.GetContentResult(context.Result, "FIRST: ");
}
}
示例12: IsValidResult
private bool IsValidResult(ActionExecutedContext context)
{
var contextResult = context.Result;
return contextResult is RedirectResult ||
contextResult is RedirectToRouteResult ||
contextResult is RedirectToActionResult;
}
示例13: OnActionExecuted
public void OnActionExecuted(ActionExecutedContext context)
{
context.Result = new ContentResult()
{
Content = "4",
ContentType = new MediaTypeHeaderValue("text/plain")
};
}
示例14: OnActionExecuted
public void OnActionExecuted(ActionExecutedContext context)
{
context.Result = new ContentResult()
{
Content = "4",
ContentType = "text/plain"
};
}
示例15: OnActionExecuted
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
var model = ((Controller)filterContext.Controller).ViewData.Model;
object viewModel = Mapper.Map(model, SourceType, DestType);
((Controller)filterContext.Controller).ViewData.Model = viewModel;
}