gpt4 book ai didi

C# mvc4 ViewResult 如何设置 Controller 和区域?

转载 作者:太空宇宙 更新时间:2023-11-03 20:02:18 28 4
gpt4 key购买 nike

public override void OnException(ExceptionContext filterContext) {

string controllerName = (string) filterContext.RouteData.Values["controller"];
string actionName = (string) filterContext.RouteData.Values["action"];
// Preserve old ViewData here
var viewData = new ViewDataDictionary<HandleErrorInfo>(filterContext.Controller.ViewData);
// Set the Exception information model here
viewData.Model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
filterContext.Result = new ViewResult { ViewName = this.View, MasterName = this.Master, ViewData = viewData, TempData = filterContext.Controller.TempData };
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.Clear();
//filterContext.HttpContext.Response.StatusCode = 500;
filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;

但是我得到错误:

{System.InvalidOperationException: The view 'Error' or its master was not found or no view engine supports the searched locations. The following locations were searched:
xxxxxxxx

但我的位置不是搜索。错误方法在 MyErrorController 中。如何显式设置 Controller (MyErrorController)

我的路径必须是~/Areas/Public/Views/MyError/Error.ascx

"area" -> "Public"
"controller", "MyError"
"action" -> "Error";

最佳答案

在设置filterContext.Result之前,尝试添加以下几行:

filterContext.RouteData.DataTokens["area"] = "Public";
filterContext.RouteData.Values["controller"] = "MyError";
filterContext.RouteData.Values["action"] = "Error";

关于C# mvc4 ViewResult 如何设置 Controller 和区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26657809/

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