2016-08-15 53 views
1

正在使用NLOG進行日誌記錄,並且日誌記錄發生時存在單獨的class @ global級別。根據要求,在日誌記錄結束後,必須重定向到該類的「錯誤」視圖(Error.cshtml)。是否有可能重定向到MVC中的非控制器類的視圖

但它是一個非控制器類,因此不能使用像RedirectToAction()或簡單地return View("Error")通常的方法。

有沒有辦法做到這一點?我試過Response.Redirect(),但什麼也沒做。

HttpContext.Current.Response.Redirect("/Help/Error",true); 

Error.cshtml是一個純HTML文件中包含文本有一些錯誤...請聯繫管理員這是Views/Shared/*文件夾下存在。

日誌記錄類存在於根文件夾下的單獨文件夾中,例如logging

每次調用Action方法時,如果發生任何異常,那麼記錄器會自動調用,這是記錄的東西,最後應該重定向到錯誤視圖。

+1

需要提供有關你如何捕獲錯誤的詳細信息指定了不同的看法。代碼路徑是什麼讓你到這個日誌代碼。 – AaronLS

+0

@AaronLS,包括這些信息。請參閱後期修改。 – Rahul

+1

是否有你想在外部類中使用該代碼的原因?你不能將代碼從該類移動到(基本)控制器中的'OnException'事件嗎? – Shyju

回答

2

您可以創建你自己的基本控制器和onException的事件

public class BaseController : Controller 
{ 
    protected override void OnException(ExceptionContext filterContext) 
    { 
     //Do your logging 
     // and redirect/return error view 
     filterContext.ExceptionHandled = true; 
     // If the exception occured in an ajax call. Send a json response back 
     // (you need to parse this and display to user as needed at client side) 
     if (filterContext.HttpContext.Request.Headers["X-Requested-With"]=="XMLHttpRequest") 
     { 
      filterContext.Result = new JsonResult 
      { 
       JsonRequestBehavior = JsonRequestBehavior.AllowGet, 
       Data = new { Error = true, Message = filterContext.Exception.Message } 
      }; 
      filterContext.HttpContext.Response.StatusCode = 500; // Set as needed 
     } 
     else 
     { 
      filterContext.Result = new ViewResult { ViewName = "Error.cshtml" }; 
      //Assuming the view exists in the "~/Views/Shared" folder 
     } 
    } 
} 

現在您的其他控制器處理例外,從這個bascontroller繼承。

public class ProductsController : BaseController 
{ 
    public ActionResult Die() 
    { 
    throw new Exception("I lost!"); 
    } 
} 

如果你想要做一個重定向(新GET調用)到您的錯誤操作方法,你可以替換爲RedirectToRouteResult中的ViewResult。

filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary { 
               {"controller", "Home"}, {"action", "Error"} 
               }; 
+0

對不起,但不能更改代碼,因爲它是現有的代碼,但絕對可以幫助其他雖然...好的建議。 – Rahul

+0

@Rahul您所說的只是「記錄器被自動調用」,所以我們會在沒有這些信息的情況下推薦解決方案。 – AaronLS

+0

@AaronLS,我會在後期添加關於該信息。它是由別人寫的很久以前,我不明白MVC – Rahul

1

你不會說如何自動調用日誌代碼「自動調用記錄器」。所以我要推薦一個HandleErrorAttribute。這需要你在你的共享文件夾視圖有Error.cshtml,但你可以在filterContext.Result

public class CustomErrorAttribute : HandleErrorAttribute 
{ 

    public override void OnException(ExceptionContext filterContext) 
    { 
     filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;// This causes the webconfig httpErrors section to be ignored, since we are handling the exception 
     filterContext.ExceptionHandled = true; 

     //... log error 

     filterContext.HttpContext.Response.ClearContent();// Removes partially rendered content 

     // just information to be passed in the view model, this does NOT define which view is displayed next. It tells us where the error came from. 
     var controllerName = (string)filterContext.RouteData.Values["controller"]; 
     var actionName = (string)filterContext.RouteData.Values["action"]; 
     var model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName); 
     // This presents an error page view. By default if a View is not passed to the Attribute, then this is Error.cshtml in Shared. 
     filterContext.Result = new ViewResult 
     { 
      ViewName = View, // View & Master are parameters from the attribute e.g. [ErrorAttribute(View="OtherErrorView")], by default the view is the "Error.cshtml" view 
      MasterName = Master, 
      ViewData = new ViewDataDictionary<HandleErrorInfo>(model), 
      TempData = filterContext.Controller.TempData 
     }; 

     filterContext.Exception = null; 


    } 
} 
+0

對不起,很長時間以來,我們不斷讚賞這個不錯的答案。 – Rahul

相關問題