gpt4 book ai didi

c# - 如何在 Asp.Net Core MVC 的 ExceptionFilter 中返回带有模型的 View

转载 作者:行者123 更新时间:2023-11-30 18:15:54 27 4
gpt4 key购买 nike

我已经创建了一个 Asp.Net Core MVC 应用程序。我想处理两种类型的错误。

我创建了两个异常:UserFriendlyExceptionUserFriendlyViewException

我已尝试创建我需要根据这些规则处理这两个异常的 ExceptionFilter:

如果异常 UserFriendlyViewException 被调用,那么我想返回带有原始 ViewName 和 AddModelError 的 ViewResult 并返回原始模型。

如果异常 UserFriendlyException 被调用,那么我想重定向到 Error View 。

这是我的ExceptionFilterAttribute:

public class ControllerExceptionFilterAttribute : ExceptionFilterAttribute
{
private readonly ITempDataDictionaryFactory _tempDataDictionaryFactory;
private readonly IModelMetadataProvider _modelMetadataProvider;

public ControllerExceptionFilterAttribute(ITempDataDictionaryFactory tempDataDictionaryFactory,
IModelMetadataProvider modelMetadataProvider)
{
_tempDataDictionaryFactory = tempDataDictionaryFactory;
_modelMetadataProvider = modelMetadataProvider;
}
public override void OnException(ExceptionContext context)
{
if (!(context.Exception is UserFriendlyException) && !(context.Exception is UserFriendlyViewException)) return;

var tempData = _tempDataDictionaryFactory.GetTempData(context.HttpContext);
//CreateNotification(NotificationHelpers.AlertType.Error, tempData, context.Exception.Message);
if (!tempData.ContainsKey(NotificationHelpers.NotificationKey)) return;

if (context.Exception is UserFriendlyViewException userFriendlyViewException)
{
context.ModelState.AddModelError(userFriendlyViewException.ErrorKey, userFriendlyViewException.Message);
}

if (context.ActionDescriptor is ControllerActionDescriptor controllerActionDescriptor)
{
//How pass here Model from context??
//If exists more views with same name but in another controller how pass correct ViewName?
var result = new ViewResult
{
ViewName = context.Exception is UserFriendlyViewException ?
controllerActionDescriptor.ActionName
: "Error",
TempData = tempData,
ViewData = new ViewDataDictionary(_modelMetadataProvider, context.ModelState)
{
{"Notifications", tempData[NotificationHelpers.NotificationKey] },
}
};

context.ExceptionHandled = true;
context.Result = result;
}

tempData.Remove(NotificationHelpers.NotificationKey);
}
}

我有两个问题:

1.) 如何将原始 ModelExceptionContext 传递到 ViewResult

2.) 如果在另一个 Controller 中存在更多具有相同名称的 View ,如何为 UserFriendlyViewException 返回正确的 ViewName?

最佳答案

如何将原始模型从 ExceptionContext 传递到 ViewResult?

您可以使用 context.ModelState 集合。

 foreach(var item in context.ModelState)
{
string parameter = item.Key;
object rawValue = item.Value.RawValue;
string attemptedValue = item.Value.AttemptedValue;

System.Console.WriteLine($"Parameter: {parameter}, value: {attemptedValue}");
}

请注意,集合将仅包含绑定(bind)参数。


如果在另一个 Controller 中存在更多具有相同名称的 View ,如何为 UserFriendlyViewException 返回正确的 ViewName?

View discovery框架将在 Controller 的操作中使用流程,因此您可以指定路径而不是 View 名称:

A view file path can be provided instead of a view name. If using an absolute path starting at the app root (optionally starting with "/" or "~/"), the .cshtml extension must be specified:

return View("Views/Home/About.cshtml");

You can also use a relative path to specify views in different directories without the .cshtml extension. Inside the HomeController, you can return the Index view of your Manage views with a relative path:

return View("../Manage/Index");

Similarly, you can indicate the current controller-specific directory with the "./" prefix:

return View("./About");

关于c# - 如何在 Asp.Net Core MVC 的 ExceptionFilter 中返回带有模型的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47454921/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com