Friday 8 January 2010

MVC: Handling controller's unknown action using "HandleUnknownAction" method.

Hi,

Welcome back.

Whenever the user for a action / method which does not exists in any controller, the framework shows the error message. How to handle it?

Each controller has one override method named "HandleUnknownAction". You can use it to handle the unknown request. The method accept one parameter actionname.

        /// Method to process unknown action request.
        /// Unknwoan action name

        protected override void HandleUnknownAction(string actionName)
        {
            ViewData["actionName"] = actionName;
            View("Error").ExecuteResult(this.ControllerContext);
        }

You can modify the Error.aspx to show the appropriate message as shown below:


        Sorry, I don't recognize the action <b> <%= Html.Encode(ViewData["actionName"]) %> </b>.


Keep watching my blog for more...

No comments:

Post a Comment