HandleUnknownAction in MVC3 Application With Error Handling

In this article I am sharing my thoughts of the advantages of HandleUnknownAction in MVC application and the terminology being used.

HandleUnknownAction

This mechanism is used to examine an incoming URL and then decide which controller and action is to be executed. Called when a request matches this controller, but no method with the specified action name is found in the controller.

Open your Visual Studio, click on "File" -> "New" -> "Project..." and select ASP.NET MVC3 Web Application; see:

jQuery1.jpg

Specify the name of your first application as you desire and click "Ok".

A new window will appear, from that window I chose Intranet application from the many options there. Another intersting fact is that there is a dropdown named "View Engine". I seleccted "Razor" that is more specific to MVC 3 and MVC 4 and kept the checkbox "Create a new unit test" unchecked.

jQuery2.jpg


Click on the "Ok" button.

The following depicts the default screen athat appears after clicking the "OK" button with multiple folders.

jQuery3.jpg

I've registered a route system in such a way as:

jQuery4.jpg


The following example shows how to render views that do not have a matching ActionResult method. For example, if you have a Verification.cshtml view but no corresponding method exists that returns an ActionResult instance, the following example displays the Verification.cshtml view when a request to the Verification action is made on the controller. If there is no matching view then the error page displays a message. This example shows one way to use the HandleUnknownActionmethod.

If you notice, I've defined a default route in global.asax that first calls to action = "Verification" of the controller named Register.

  routes.MapRoute(

                "Default", // Route name

                "{controller}/{action}/{id}", // URL with parameters

                new { controller = "Register", action = "Verification", id = UrlParameter.Optional }  // Parameter defaults

            );

I run my application and it navigtes to the following URL: http://localhost:53098/.

It looks for an action method Verification that doesn't exist in the Register controller. Whenever any request (URL) doesn't find an associated action name in the controller it sends control to "HandleUnknownAction" (as the name implies it handles the Unknown action request). It fetches the name of the action from the URL and set it to the string actionName in "HandleUnknownAction(string actionName)" and redirects to the desired View().

HandleUnknownAction can be used for error handling purposes and to redirect a specific page on demand.

jQuery5.jpg


jQuery6.jpg

Somehow if any error occurs during execution of the method then it jumps into the catch block and proceeds to error.cshtml present in the shared folder.

For example if I use the the URL "http://localhost:53098/Register/Verfification1" in the browser and press Enter then it shows me an error and the result would be as in the following.

Due to this is, neither Verification action methods exist in the Register controller nor the Verification.cshtml View.

jQuery7.jpg

Note:

jQuery8.jpg

You can use both of these ViewData properties in View like this:

jQuery9.jpg

The sample application is attached as a reference.

Enjoy MVC and Coding.

Kindly inform me if you have any query.

Up Next
    Ebook Download
    View all
    Learn
    View all