2011-06-12 19 views
46

पर प्रचार करता रहता है, मैं एक दृश्य वापस करने की कोशिश कर रहा हूं, जो कि मेरे एप्लिकेशन से होने वाली कुछ त्रुटियों के आधार पर उपयोगकर्ता को रीडायरेक्ट जारी नहीं करता है, मैं त्रुटियों को संभालना चाहता हूं + उन्हें लॉग इन करना चाहता हूं मेरे बेस कंट्रोलर के अंदर, मैं नहीं चाहता कि त्रुटि मेरे ग्लोबल.एक्सएक्स - एप्लीकेशन_इरर() विधि को प्रचारित करे, क्योंकि मैं इस विधि को अपने ऐप के अंदर किसी भी अन्य त्रुटियों को संभालने के लिए चाहता हूं। उपयोगकर्ता एक फर्जी यूआरएल में प्रवेश करता है, क्या किसी को इसके चारों ओर एक रास्ता मिल गया है?एएसपीनेट एमवीसी ओवरराइड बेस कंट्रोलर में ऑनएक्सप्शन एप्लिकेशन_इरर

नोट: के रूप में मैं कुछ मुद्दों के लिए एक समाधान के लिए किया था मैं अपने टिप्पणी की कोड को छोड़ दिया है, यह भी पता चलता है मैं संभव संभाल करने के लिए कई अपवाद है ...

संपादित करें: यदि मैं भीतर इस OnException सब कुछ ओवरराइड एक RedirectToAction जारी अपेक्षा के अनुरूप काम करता है, लेकिन मैं केवल देख सकते हैं और रीडायरेक्ट न लौटना चाहते ...

मेरे आधार नियंत्रक विधि है:

protected override void OnException(ExceptionContext filterContext) 
    { 
     //dont interfere if the exception is already handled 
     if (filterContext.ExceptionHandled) 
      return; 

     //let the next request know what went wrong 
     filterContext.Controller.TempData["exception"] = filterContext.Exception; 

     //log exception 
     _logging.Error(User.Identity.Name, ExceptionHelper.BuildWebExceptionMessage(filterContext.Exception)); 


     //set up redirect to my global error handler 
     //if (filterContext.Exception.GetType() == typeof(NoAccessException)) 
     // filterContext.Result = View(new RouteValueDictionary 
     // (new { area = "", controller = "Error", action = "PublicError" })); 

     //else { 
     //Only return view, no need for redirection 
     filterContext.Result = View(new RouteValueDictionary 
     (new { area = "", controller = "Error", action = "NoAccess" })); 
     //} 
     //advise subsequent exception filters not to interfere and stop 
     // asp.net from showing yellow screen of death 
     filterContext.ExceptionHandled = true; 

     //erase any output already generated 
     filterContext.HttpContext.Response.Clear(); 

     //base.OnException(filterContext); 
    } 

इस विधि किसी अन्य त्रुटियों है कि मेरे ऐप में दिखाई दे सकता संभाल चाहिए, मैं ई नहीं चाहते हैं ऊपर rrors मेरी Application_Error() के अंदर संभाला जा रहा

protected void Application_Error() 
     { 

      Exception exception = Server.GetLastError(); 
      // Log the exception. 

      var logger = Container.Get<ILoggingService>(); 
      logger.Error(User.Identity.Name, ExceptionHelper.BuildWebExceptionMessage(exception)); 

      Response.Clear(); 

      HttpException httpException = exception as HttpException; 

      RouteData routeData = new RouteData(); 
      routeData.Values.Add("controller", "Error"); 

      //if (httpException == null) 
      //{ 
      routeData.Values.Add("action", "PublicError"); 
      //} 
      //else //It's an Http Exception, Let's handle it. 
      //{ 
      // switch (httpException.GetHttpCode()) 
      // { 
      //  case 404: 
      //   // Page not found. 
      //   routeData.Values.Add("action", "HttpError404"); 
      //   break; 
      //  case 500: 
      //   // Server error. 
      //   routeData.Values.Add("action", "HttpError500"); 
      //   break; 

      //  // Here you can handle Views to other error codes. 
      //  // I choose a General error template 
      //  default: 
      //   routeData.Values.Add("action", "General"); 
      //   break; 
      // } 
      //} 

      // Pass exception details to the target error View. 
      routeData.Values.Add("error", exception); 

      // Clear the error on server. 
      Server.ClearError(); 

      // Avoid IIS7 getting in the middle 
      Response.TrySkipIisCustomErrors = true; 

      // Call target Controller and pass the routeData. 
      IController errorController = new ErrorController(); 
      errorController.Execute(new RequestContext(
       new HttpContextWrapper(Context), routeData)); 
     } 

उत्तर

64

निम्नलिखित काम करना चाहिए:

protected override void OnException(ExceptionContext filterContext) 
{ 
    if (filterContext.ExceptionHandled) 
    { 
     return; 
    } 
    filterContext.Result = new ViewResult 
    { 
     ViewName = "~/Views/Shared/Error.aspx" 
    }; 
    filterContext.ExceptionHandled = true; 
} 

भी सुनिश्चित करें कि कोई अपवाद नहीं इस विधि में फेंक दिया जाता है या यह Application_Error तक पहुंचने में होगा।

+0

क्या यूआरएल मूल रूप से रहेगा ... ??? यही वह है जो मैं उपर्युक्त विधि से प्राप्त करने की कोशिश कर रहा हूं यानी यदि कोई त्रुटि होती है तो प्रासंगिक दृश्य वापस भेजा जाता है और यूआरएल वही रहता है/यूआरएल नहीं बदलता है ... – Haroon

+1

@ हरून, हाँ बिल्कुल, यह वही रहेगा । यह प्रारंभिक यूआरएल होगा जो अपवाद का कारण बनता है, सिवाय इसके कि मौत की पीले रंग की स्क्रीन के बजाय कस्टम व्यू प्रस्तुत किया जाएगा। –

+0

धन्यवाद, यह काम करता है जैसा मैं चाहता था! : पी – Haroon

संबंधित मुद्दे