संक्षेप में, जाने का रास्ता HandleErrorAttribute विस्तार करने के लिए, इस तरह हो सकता है:
public class OncHandleErrorAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext context)
{
// Elmah-Log only handled exceptions
if (context.ExceptionHandled)
ErrorSignal.FromCurrentContext().Raise(context.Exception);
if (context.HttpContext.Request.IsAjaxRequest())
{
// if request was an Ajax request, respond with json with Error field
var jsonResult = new ErrorController { ControllerContext = context }.GetJsonError(context.Exception);
jsonResult.ExecuteResult(context);
context.ExceptionHandled = true;
}
else
{
// if not an ajax request, continue with logic implemented by MVC -> html error page
base.OnException(context);
}
}
}
निकालें Elmah प्रवेश कोड लाइन यदि आप इसे ज़रूरत नहीं है। मैं एक त्रुटि और संदर्भ के आधार पर एक जेसन वापस करने के लिए अपने नियंत्रकों में से एक का उपयोग करता हूं। यहां नमूना है:
public class ErrorController : Controller
{
public ActionResult GetJsonError(Exception ex)
{
var ticketId = Guid.NewGuid(); // Lets issue a ticket to show the user and have in the log
Request.ServerVariables["TTicketID"] = ticketId.ToString(); // Elmah will show this in a nice table
ErrorSignal.FromCurrentContext().Raise(ex); //ELMAH Signaling
ex.Data.Add("TTicketID", ticketId.ToString()); // Trying to see where this one gets in Elmah
return Json(new { Error = String.Format("Support ticket: {0}\r\n Error: {1}", ticketId, ex.ToString()) }, JsonRequestBehavior.AllowGet);
}
मैं ऊपर कुछ टिकट जानकारी जोड़ता हूं, आप इसे अनदेखा कर सकते हैं। तरीके के कारण फिल्टर लागू किया गया है (डिफ़ॉल्ट HandleErrorAttributes फैली) करने के लिए हम वैश्विक फिल्टर से तो HandleErrorAttribute निकाल सकते हैं:
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new GlobalAuthorise());
filters.Add(new OncHandleErrorAttribute());
//filters.Add(new HandleErrorAttribute());
}
यह मूलतः यह है। अधिक विस्तृत जानकारी के लिए आप my blog entry पढ़ सकते हैं, लेकिन विचार के लिए, ऊपर पर्याप्त होना चाहिए।
स्रोत
2012-08-15 11:23:01
मैंने यह कोशिश की है, लेकिन जब हैंडलजेसन एररएट्रिब्यूट ऑनएक्सप्शन विधि को फ़िल्टरकॉन्टेक्स्ट कहा जाता है। अपवाद हैडलड संपत्ति हमेशा सत्य होती है जब नियंत्रक वर्ग पर हैंडलरर एट्रिब्यूट होता है तो कार्रवाई संबंधित होती है। क्या कार्रवाई विधियों को आतंकवाद को प्राथमिकता नहीं लेनी चाहिए और पहले कहा जाना चाहिए? –