2011-03-05 8 views
6

मैं एक कस्टम अपवाद फ़िल्टर बनाना चाहता हूं जो जेएसओएन परिणामों को वापस करने वाले नियंत्रक कार्यों में फेंकने वाले अपवादों को पकड़ लेगा।कस्टम अपवाद फ़िल्टर से JSON परिणाम कैसे वापस करें?

मैं निम्नलिखित कार्रवाई विधि refactor करने के लिए करना चाहते हैं:

 public JsonResult ShowContent() 
    { 
     try 
     { 
      // Do some business logic work that might throw a business logic exception ... 
      //throw new ApplicationException("this is a business exception"); 

      var viewModel = new DialogModel 
           { 
            FirstName = "John", 
            LastName = "Doe" 
           }; 

      // Other exceptions that might happen: 
      //throw new SqlException(...); 
      //throw new OtherException(...); 
      //throw new ArgumentException("this is an unhandeled exception"); 

      return 
       Json(
        new 
         { 
          Status = DialogResultStatusEnum.Success.ToString(), 
          Page = this.RenderPartialViewToString("ShowContent", viewModel) 
         }); 
     } 
     catch (ApplicationException exception) 
     { 
      return Json(new { Status = DialogResultStatusEnum.Error.ToString(), Page = exception.Message }); 
     } 
     catch (Exception exception) 
     { 
      return Json(new { Status = DialogResultStatusEnum.Exception.ToString(), Page = "<h2>PROBLEM!</h2>" }); 
     } 
    } 
} 

मुझे क्या करना चाहते हैं एक कस्टम अपवाद फ़िल्टर विशेषता है कि कार्रवाई में फेंक दिया किसी भी अपवाद पकड़ेगा बनाने है निम्न तर्क का पालन करें:

  1. चेक अगर वहाँ एक अपवाद
    • था नहीं: वापसी
    • हाँ:
      • BusinessLogic अपवाद है - एक JSON परिणाम लौट
      • तो अन्य बिना क्रिया का अपवाद:
        • लॉग
        • वापसी एक अलग परिणाम कोड के साथ एक और JSON परिणाम

उत्तर

-2
public class YourController : BaseController 
    { 
     public JsonResult showcontent() 
     { 
      // your logic here to return foo json 
      return Json (new { "dfd" }); // just a dummy return text replace it wil your code 
     } 
    } 

    public class BaseController : Controller 
    { 
// Catch block for all exceptions in your controller 
     protected override void OnException(ExceptionContext filterContext) 
     { 
      base.OnException(filterContext); 
      if (filterContext.Exception.Equals(typeof(ApplicationException))) 
      { 
       //do json and return 
      } 
      else 
      { 
       // non applictaion exception 
// redirect to generic error controllor and error action which will return json result 

      } 
     } 

    } 

इस लिंक का संदर्भ लें बना सकते हैं और कार्रवाई के लिए HandleAttribute के लिए HandleError attribute

* संपादित उपयोग करने के लिए कैसे *

//not tested code 
public class HandleErrorfilter : HandleErrorAttribute 
    { 
     public string ErrorMessage { get; set; } 

     public override void OnException(ExceptionContext filterContext) 
     { 
       string message = string.Empty; 
       //if application exception 
       // do something 
       else 
        message = "An error occured while attemting to perform the last action. Sorry for the inconvenience."; 
      } 
      else 
      { 
       base.OnException(filterContext); 
      } 
     } 

     public class YourController : BaseController 
     { 
      [HandleErrorfilter] 
      public JsonResult showcontent() 
      { 
       // your logic here to return foo json 
       return Json (new { "dfd" }); // just a dummy return text replace it wil your code 
      } 
     } 
+0

धन्यवाद स्वैपनेल लेकिन मैं फ़िल्टर विशेषता बनाना चाहता हूं मैं अपने नियंत्रक कार्यों को सजाने के लिए उपयोग करूंगा जो JSON लौटाते हैं - मैं ऑनएक्सप्शन विधि को ओवरराइड करने में नहीं देख रहा हूं। – Elie

+0

आपने अपने प्रश्न में कस्टम अपवाद फ़िल्टर विशेषता के लिए अनुरोध किया है? – swapneel

+0

क्रियाओं के लिए हैंडलएट्रिब्यूट के लिए मेरा * EDIT देखें * – swapneel

15

मैं इसे इस समस्या कोड this article में पाया का उपयोग कर हल करने के लिए संभव पाया देखने के लिए (इसमें मामूली परिवर्तन के साथ।)

public class HandleJsonExceptionAttribute : ActionFilterAttribute 
{ 
    #region Instance Methods 

    public override void OnActionExecuted(ActionExecutedContext filterContext) 
    { 
     if (filterContext.Exception != null) 
     { 
      filterContext.HttpContext.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError; 
      filterContext.Result = new JsonResult() 
      { 
       JsonRequestBehavior = JsonRequestBehavior.AllowGet, 
       Data = new 
       { 
        Error = filterContext.Exception.Message 
       } 
      }; 
      filterContext.ExceptionHandled = true; 
     } 
    } 

    #endregion 
} 
+2

यह सही उत्तर – CMS

+1

होना चाहिए, मैं सहमत हूं कि यह सही उत्तर होना चाहिए। हालांकि, 1.1 में एक बग (?) है जिसके लिए अपवाद को सही पर सेट करने की आवश्यकता नहीं है। यदि आप प्रतिक्रिया का शरीर खाली करेंगे। – JD987

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