mr.
mr.

Reputation: 679

How to reach parameters of method in global error handling in asp.net mvc5

I am newbie to error handling in global.asax in asp.net mvc5.In Application_Error i can reach all url but is there any way to see Post parameters of method in global.asax to give the meaning the error. In detail i have Application_Error() as below.

protected void Application_Error()
    {
        HttpContext httpContext = HttpContext.Current;

        int userid = Convert.ToInt32(httpContext.User.Identity.Name.Split('_')[1]);
        if (httpContext != null)
        {
            RequestContext requestContext = ((MvcHandler)httpContext.CurrentHandler).RequestContext;

            if (requestContext.HttpContext.Request.IsAjaxRequest())
            {


                httpContext.Response.Clear();
                string controllerName = requestContext.RouteData.GetRequiredString("controller");
                IControllerFactory factory = ControllerBuilder.Current.GetControllerFactory();
                IController controller = factory.CreateController(requestContext, controllerName);
                ControllerContext controllerContext = new ControllerContext(requestContext, (ControllerBase)controller);
                JsonResult jsonResult = new JsonResult();
                jsonResult.Data = new { Success = false, Message = "Error" };
                jsonResult.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
                jsonResult.ExecuteResult(controllerContext);
                httpContext.Response.End();
            }

        }
    }

What if you take an error in Method like below

  public JsonResult(ExampleModel example){
    return Json("");}

Is there any way to reach the properties of ExampleModel class in Application_Error()?

Upvotes: 0

Views: 743

Answers (1)

Andrei
Andrei

Reputation: 56698

I am not sure if there is an out of the box way to get the whole ExampleModel object in global.asax, but for sure you can access individual request parameters that this object consists of. Say if the model has property ExampleProperty which was posted with request, this should give you its value:

requestContext.HttpContext.Current.Request["ExampleProperty"]

Upvotes: 2

Related Questions