29.05.2015 Views

o_19mgorv9t13a3ko71fev19l81mqa.pdf

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Table 18-9. HandleErrorInfo Properties<br />

Name Type Description<br />

ActionName string Returns the name of the action method that generated the exception<br />

ControllerName string Returns the name of the controller that generated the exception<br />

Exception Exception Returns the exception<br />

You can see how I have updated the RangeError.cshtml view to use this model object in Listing 18-30.<br />

Listing 18-30. Using a HandleErrorInfo Model Object in the RangeError.cshtml File<br />

@model HandleErrorInfo<br />

@{<br />

}<br />

ViewBag.Title = "Sorry, there was a problem!";<br />

<br />

<br />

<br />

<br />

Range Error<br />

<br />

<br />

Sorry<br />

The<br />

@(((ArgumentOutOfRangeException)Model.Exception).ActualValue)<br />

was out of the expected range.<br />

<br />

@Html.ActionLink("Change value and try again", "Index")<br />

<br />

<br />

<br />

value<br />

I have to cast the value of the Model.Exception property to the ArgumentOutOfRangeException type to<br />

be able to read the ActualValue property because the HandleErrorInfo class is a general-purpose model object<br />

that is used to pass any exception to a view.<br />

Using Action Filters<br />

Action filters are filters that can be used for any purpose. The built-in class for creating this kind of filter, IActionFilter,<br />

is shown in Listing 18-31.<br />

Listing 18-31. The IActionFilter Interface<br />

namespace System.Web.Mvc {<br />

}<br />

public interface IActionFilter {<br />

void OnActionExecuting(ActionExecutingContext filterContext);<br />

void OnActionExecuted(ActionExecutedContext filterContext);<br />

}<br />

484

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!