ASP.NET MVC Ajax Actions results enveloping


ASP.NET MVC Ajax Actions results enveloping



I would like to have every result of any AJAX call on ASP.NET MVC to be enveloped to a JSON object which should be like: AjaxResult { status, data }

where status will contain a enumeration value describing if the call was successful, erroneous, authentication expired etc. This will enable client side code to be able to redirect to the login page etc.

I tried catching Ajax Requests by overriding OnActionExecuted, and trying to render the returned by the corresponding action result using the following code, but this solution seems operating slow. Do you have some better idea?

protected override void OnActionExecuted(ActionExecutedContext filterContext) {     if (filterContext.HttpContext.Request.IsAjaxRequest() && filterContext.Exception == null)     {         if (filterContext.Result.GetType() == typeof(ViewResult))         {             ViewResult viewResultTemp = (ViewResult)filterContext.Result;             using (StringWriter sw = new StringWriter())             {                 ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewResultTemp.ViewName);                 ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);                 viewResult.View.Render(viewContext, sw);                 var ajaxReply = new AjaxReply(AjaxReplyStatus.Success, string.Empty, sw.ToString());                 filterContext.Result = new JsonResult {Data = ajaxReply};             }         }         else if (filterContext.Result.GetType() == typeof(PartialViewResult))         {             PartialViewResult partialViewResultTemp = (PartialViewResult)filterContext.Result;             using (StringWriter sw = new StringWriter())             {                 ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, partialViewResultTemp.ViewName);                 ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);                 viewResult.View.Render(viewContext, sw);                 var ajaxReply = new AjaxReply(AjaxReplyStatus.Success, string.Empty, sw.ToString());                 filterContext.Result = new JsonResult { Data = ajaxReply };             }         }         else if (filterContext.Result.GetType() == typeof(JsonResult))         {             JsonResult jsonResult = (JsonResult)filterContext.Result;             JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();             string jsonData = javaScriptSerializer.Serialize(jsonResult.Data);             var ajaxReply = new AjaxReply(AjaxReplyStatus.Success, string.Empty, jsonData);             filterContext.Result = new JsonResult { Data = ajaxReply };         }     } } 

When to use Singleton vs Transient vs Request using Ninject and MongoDB

1:

ASP.NET MVC Registration With Captcha
Why you need this? Create your custom ApplicationController and derive all controllers from this one. ASP.NET MVC: Why is `ToMvcHtmlString` not public?In ApplicationController implement the method Json<data>() where data. What is a good strategy for dealing with large javascript code in asp.net MVC projects?
   public JsonResult Json<TData>(TData data, bool status) where TData : class     {         return Json(             new                 {                     data,                     status                 },             JsonRequestBehavior.AllowGet);     } 
How to NOT route files & directories to ASP.NET MVCObject gets disconnected from nHibernate session after TryUpdateModel is called

2:

Validating that a form input is not empty
Do you really need to did this at all?. Value from Hidden Input field as an Actionlink Parameter If your ajax call succeeds than HTTP 200 will be returned and your success jQuery callback will be called. If your call fails than just throw an exception and let jQuery call error callback after it received HTTP 500 from the server.. HTTP status codes are the proper way to inform the caller if the call has succeeded or break down for a certain reason..


73 out of 100 based on 43 user ratings 473 reviews

@