1

I do have created an Error View for a MVC C# App, and it is very simple but I can manage to show the controller, actioin and the message from where the exception happens(I need it for develepment purposes) But it always throw an exception in code. this is my global.asax

   public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        protected void Application_Error(object sender, EventArgs e)
        {
            Exception exc = Server.GetLastError();
            Server.ClearError();
            Response.Redirect("/ErrorPage/ErrorMessage");
        }

this is my ErrorrPageController

 public class ErrorPageController : Controller
    {
        public ActionResult ErrorMessage()
        {
            return View();
        }
    }

and this is the view that throws the error, it throws errors in @Model.ControllerName, @Model.ActionName and @Model.Exception.Message

@model System.Web.Mvc.HandleErrorInfo 
<div class="container">

    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <div>
                <br />
                <div class="form-group">
                    <div class="row">
                        <div class="col-md-12">
                            <img src="~/Imagenes/logo.png" class="img-responsive center-block" />
                        </div>
                    </div>
                    <h2>Ooops an error has been triggered</h2>
                    <p>Controller = @Model.ControllerName</p>
                    <p>Action = @Model.ActionName</p>
                    <p>Message = @Model.Exception.Message</p>
                </div>
                @*<hr />*@
                <br />


                <div class="form-group">
                    <div class="col-md-12">
                        <a href="@Url.Action("TipoEvento", "Home")" class="pull-right linkedin-link">Regresar <i class="fa fa-angle-right"></i></a>
                    </div>
                </div>

            </div>

        </div>
    </div>
</div>

and this is the error that throws

enter image description here

But I really need to show those info(again, for dev purposes), so, could you please help me and tell how to show the detailed error info in the error page

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
Pablo Tobar
  • 614
  • 2
  • 13
  • 37
  • Possible duplicate of [How to make custom error pages work in ASP.NET MVC 4](https://stackoverflow.com/questions/13905164/how-to-make-custom-error-pages-work-in-asp-net-mvc-4) – Panagiotis Kanavos Jun 28 '17 at 15:29
  • Use `try{} catch(Exception ex){}`, then redirect to the page. You can use `TempData` to store your exception, or another method for storing if you prefer – lloyd Jun 28 '17 at 17:27

2 Answers2

0

I'd write a custom Error Handler attribute and apply it globally. Here's one I wrote to specifically trap authorization exceptions and send them to a specific page. The main thing is grabbing the action and controller information from the ExceptionContext.

public class HandleUnauthorizedAttribute : HandleErrorAttribute
{
    public override void OnException(ExceptionContext filterContext)
    {
        base.OnException(filterContext);

        //remove the following line to capture all exceptions.  this only lets Security exceptions through
        if (filterContext.Exception.GetType() != typeof(SecurityException)) return;

        var controllerName = (string)filterContext.RouteData.Values["controller"];
        var actionName = (string)filterContext.RouteData.Values["action"];
        var model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);

        filterContext.Result = new ViewResult
        {
            //name your view whatever you want and place a matching view in /Views/Shared
            ViewName = "Unauthorized",
            ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
            TempData = filterContext.Controller.TempData
        };
        filterContext.ExceptionHandled = true;
        filterContext.HttpContext.Response.Clear();
        filterContext.HttpContext.Response.StatusCode = 403;
        filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
    }
}

Register the new attribute in FilterConfig.cs

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleUnauthorizedAttribute());
    }
}

Create a view in your Views/Shared directory that matched the ViewName from the filter.

Fran
  • 6,440
  • 1
  • 23
  • 35
  • yes, I´already modified the FilterConfig but still don´t know where to paste the code you posted and in my case I´already have a controller: ErrorPageController and an action: ErrorMessage, where should I define the controller/action in your code? – Pablo Tobar Jun 28 '17 at 16:21
  • The attribute is just a class. Drop it in a directory call /Attributes. You don't need an ErrorPageController or action. The ActionFilter will send you to the view. – Fran Jun 28 '17 at 16:25
  • have you debugged it? is the filter getting hit? – Fran Jun 28 '17 at 16:50
-1

Hi Its seems that you just need to add an entry like below to the Application start :

   protected void Application_Start()
    {       
        AreaRegistration.RegisterAllAreas();

       //Here is the entry
        RegisterGlobalFilters(GlobalFilters.Filters);


        RegisterRoutes(RouteTable.Routes);

        ModelBinders.Binders.DefaultBinder = new DevExpress.Web.Mvc.DevExpressEditorsBinder();
    }

Note: In the above of your code the model was null that why you got the error.

In this way, it will automatically send the error model to the view.

Source/Usefullink:https://stackoverflow.com/a/21392400/3397630

Karthik Elumalai
  • 1,574
  • 1
  • 11
  • 12