【发布时间】:2018-12-02 17:55:58
【问题描述】:
我正在使用 ASP .NET Core 2.0 中的 WEB 应用程序,其中我有一个继承自 ExceptionFilterAttribute 的自定义 ExceptionAttribute 过滤器。
如何访问传递给 POST 调用中的操作的模型对象。
上述方法传递了一个 ExceptionContext,但我找不到一种简单可靠的方法来从中获取模型对象并传递给 ViewResult。
我拥有的过滤器如下所示:
public class ApiCallExceptionAttribute: ExceptionFilterAttribute
{
private readonly IModelMetadataProvider _modelMetadataProvider;
public ApiCallExceptionAttribute(
IModelMetadataProvider modelMetadataProvider)
{
_modelMetadataProvider = modelMetadataProvider;
}
public override void OnException(ExceptionContext context)
{
//how can i accesss model object here and pass to ViewResult
result.ViewData = new ViewDataDictionary(_modelMetadataProvider,context.ModelState);
context.Result = result;
}
}
控制器如下所示:
[HttpPost]
[ServiceFilter(typeof(ApiCallExceptionAttribute))]
public async Task<IActionResult> Activation(ActivationViewModel model)
{
//throw exception in controller content
}
【问题讨论】:
-
这方面有什么进展吗?
标签: c# asp.net-core exception-handling