简体   繁体   中英

C# mvc4 ViewResult how to set controller and area?

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;

But i get error:

{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

but my location is not search. Error method is inside MyErrorController. How can I explicitly set controller ( MyErrorController )

My path must be ~/Areas/Public/Views/MyError/Error.ascx

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

Before setting filterContext.Result , try to add the following lines:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM