This project is read-only.
In this scenario we are going to extend ExceptionFilterAttribute in order to handle exception of a specific type.
We assume that all contact with the Email will cause the ArgumentException to be thrown. TheArgumentException need a specific way of handling. We suppose that the way of handling consist in displaying a message to the user.

Creating CustomException handler attribute

Right click on the ./filters folder and add a new class named CustomExceptionFilterAttribute. Make sure that the class is public and extends the base ExceptionFilterAttribute provided by the framework.
Override the OnException method and put down the following code:

public override void OnException(Koossery.MVCwin.Filters.context.ExceptionContext filterContext)

    //Using the controller to store an error message
    ControllerBase controller = filterContext.Controller as ControllerBase;

    //Storing the message
    controller.AddMessage(filterContext.Exception.Message, Koossery.MVCwin.Util.MessageType.ERROR_MESSAGE);

The base implementation is first executed. Our part of work consists of adding and error message using the controller in the filterContext parameter.
The next step is to add the new created attribute to the Save action of the SaveController. The Save action is also responsible of updating a contact. The listing below shows the code:

[CustomExceptionFiler(ExceptionType = typeof(ArgumentException), ViewName = "SaveView", ExceptionHandled = true)]
public IActionResult Save()
    . . .

The annotation indicates that the CustomExceptionFilter should only care about exception of type, the type of ArgumentException. Whenever an exception of this type is caught the filter will handle properly the exception, mark the exception as handled and displays the view SaveView. Cooool!

More customization off the SaveController’s Save action

Will throw the ArgumentException if the entered email is (it is just an example).
Modify the Save action as indicates below:

[CustomExceptionFiler(ExceptionType = typeof(ArgumentException), ViewName = "SaveView", ExceptionHandled = true)]
public IActionResult Save()
    SaveViewData saveView;
        //this code is added just for testing purpose

        //Retrieve the view data
        saveView = this.GetSessionData(typeof(SaveViewData).Name) as SaveViewData;

        //reset le context data

        //Check if the Email is different of
        if (saveView.Contact.Email == "") throw new ArgumentException("This Email is not allowed, please enter another one");
        //Save the contact

        //Invoking the list action
        return RedirectToAction(typeof(ListController).Name, ApplicationData.InitAction);
    catch (Exception ex)
        if (typeof(ArgumentException).IsInstanceOfType(ex)) throw ex;
        arg = new Object[3];
        arg[0] = FrontEndConstants.TECHNICAL_DETAIL;
        arg[1] = ex.Message;

        strMsg = String.Format(XmlUtility.getKeyValueFromXmlConfigFile(errorMessageXmlFileName, "SAVECONTACT", "SAVECONTACT-001"), arg);

        String strReturnMessage = strMsg.Substring(0, strMsg.IndexOf(FrontEndConstants.TECHNICAL_DETAIL));
        this.AddMessage(strReturnMessage, MessageType.ERROR_MESSAGE);

         //display the saveView
        return this.View(typeof(SaveView).Name);

Noticed the following code in the try part of the action

//Check if the Email is different of
if (saveView.Contact.Email == "") throw new ArgumentException("This Email is not allowed, please enter another one");

The Save action has already a way of handling exception but an ArgumentException exception should handle differently. That’s why we need to throw exceptions of type ArgumentException as indicated in the below code in the catch part of the action

if (typeof(ArgumentException).IsInstanceOfType(ex)) throw ex;

More customization of the SaveView

The save view needs to display the message of the exception (saved by the custom filter) to the user. We need to add the code that displays this message. Add the following method to the SaveView

private void DisplayMessages()
    lblErrorMessage.Visible = this.GetContextVisibility();
    //On récupère le message d'erreur
    if (GetMessage(MessageType.ERROR_MESSAGE) != null)
        lblErrorMessage.ForeColor = Color.IndianRed;
        lblErrorMessage.Text = GetMessage(MessageType.ERROR_MESSAGE);
    else if (GetMessage(MessageType.OTHER_MESSAGE) != null)
        lblErrorMessage.ForeColor = Color.Green;
        lblErrorMessage.Text = GetMessage(MessageType.OTHER_MESSAGE);

The method displays a message using the built in GetMessage provided by the framework. The colour of the label depends on the type of the message.

Now edit the BindDataToView method as shown bellow

public override void BindDataToView()
    //Retrieving the SaveViewData
    SaveViewData saveView = this.GetSessionData(typeof(SaveViewData).Name) as SaveViewData;

    //Initializing Data Source
    contactBindingSource.DataSource = saveView.Contact;

    //Retrieving the ControllerBase
    ControllerManager manager = this.ControllerManager as ControllerManager;

    //Display messages

Run the application, log in (using {yacoubou, minastirite} or {bake, tochange}) and try to create or modify a contact using the email Yes of course you can't as shown in the picture bellow:



In this scenario we create a CustomExceptionFilterAttribute by extending the base ExceptionFilterAttribute provided by the framework. You can handle a specific exception the way you want, the framework provides everything you need.

Last edited Sep 1, 2009 at 11:06 AM by yacoubou, version 2


No comments yet.