0

I'm referring to Darin's answer in the following post.

I'm using MVC3 and I was wondering if this is still a way to handle errors?

In the rrror controller he has the following:

public ActionResult Http404()
{
     Response.StatusCode = 404;

     return Content("404", "text/plain");
}

Working from his code.. I want it to display the full URL, how would I do this? So it needs to be www.mywebsite.com/Error/Http404 and not just www.mywebsite.com. How would I do this? I thought that if I had the following code then it will display the full URL, but it still just displays www.mywebsite.com:

public ActionResult Http404()
{
     Response.StatusCode = 404;

     return View("Http404");
}

How would I get the value of ex that was set in the global.asax.cs to be displayed on this page?

I moved away from having my errors handled in the web.config because it kepted on have a aspxerrorpath in the querystring.

Community
  • 1
  • 1
Brendan Vogt
  • 25,678
  • 37
  • 146
  • 234
  • The fact is you can't change the URL unless you are doing a Response.Redirect from the server. – VJAI Apr 20 '12 at 13:22
  • Is this the only way to do it from the global.asax? – Brendan Vogt Apr 23 '12 at 05:31
  • Yeh! From the global.asax after handling the exception redirect to the corresponding controller action, but note that your exception will be lost in the redirect. – VJAI Apr 23 '12 at 11:59

2 Answers2

0

Have you tried: http://community.codesmithtools.com/CodeSmith_Community/b/tdupont/archive/2011/03/01/error-handling-and-customerrors-and-mvc3-oh-my.aspx

This sample does not redirect to the error page but keeps showing the url requested where the error occurred. I hope it will help you out.

amaters
  • 2,266
  • 2
  • 24
  • 44
0

So from my understanding you want to redirect the user to a particular error page on exception and also from the error page you want to access the exception.

Here is what I've tried, created a base controller and overridden the OnException method. Inside the OnException method I'm doing most of the things you are trying in the Global.asax.cs.

Most importantly after handling the exception I'm setting a RedirectAction in the Result property of the ExceptionContext.

Also to access the Exception from the error views I've stored the exception in the TempData. From the ErrorController I'm passing the exceptions as HandleErrorInfo instances to the views.

Here is the complete code.

// BaseController
public class BaseController : Controller
{
  protected override void OnException(ExceptionContext filterContext)
  {
    if (!filterContext.ExceptionHandled)
    {
      var action = "General";

      if (filterContext.Exception is HttpException)
      {
        var httpException = filterContext.Exception as HttpException;

        switch (httpException.GetHttpCode())
        {
          case 404:
            // page not found
            action = "Error404";
            break;
          case 500:
            // server error
            action = "Error500";
            break;
          default:
            action = "General";
            break;
        }
      }

      filterContext.Result = RedirectToAction(action, "error");
      TempData["error"] = new HandleErrorInfo(filterContext.Exception, 
                          RouteData.Values["controller"].ToString(), 
                          RouteData.Values["action"].ToString());
      filterContext.ExceptionHandled = true;
    }
  }
}

// Error Controller
public class ErrorController : Controller
{
  public ActionResult Error404()
  {
    return View(TempData["error"] as HandleErrorInfo);      
  }

  // other methods
}

// Sample controller derives from BaseController.
public class HomeController : BaseController
{
  public ActionResult Index()
  {
    return View();
  }

  public ActionResult NotFound()
  {
    throw new HttpException(404, "Not Found");
  }
}

// Error404.cshtml
@model System.Web.Mvc.HandleErrorInfo
@{
  ViewBag.Title = "404 Not Found";
}
<h2>@ViewBag.Title</h2>

<p>
  Last Exception: @Model.Exception
</p>

UPDATE: One disadvantage with this approach is we can handle the HTTP exceptions 404, 500 or whatever if it is thrown only from the controller actions.

VJAI
  • 32,167
  • 23
  • 102
  • 164