Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

HandleUnknownAction in MVC

0.00/5 (No votes)
21 Oct 2014 1  
Advantages and uses of Controller.HandleUnknownAction in MVC Architecture

Introduction

MVC stands for Model-View-Controller. This is an architectural design for developing the application where the application is basically divided into 3 layers and each layer has its own relevant functionalities.

View provides the look and feel to the application what exactly the end user sees. These are the template files that your application uses to dynamically generate HTML responses and files may have the extensions html, asp, aspx, cshtml, and vbhtml, depending on the language content.

Models are the classes like customer class, vendor class which contains the business logic, validation logic and data access logic. In MVC models, both hold and manipulate application data.

Controller is the heart of the application which controls the flow of application execution. It handles incoming browser requests, retrieves model data, and then specifies view templates that return a response to the browser.

Background

In MVC, browser request comes in the form of actions which are nothing but the function names in the controller class and the controller class can have many action methods defined within.

So when any browser requests an action along with the controller, the controller looks for an associated action name and returns the response in the form of views or files or might redirect to another controller action. So the question is now what if the controller doesn't find the associated action. And the answer will be it will throw the error.

Here I have added one controller class, i.e., EmpoyeeInfoController.cs. And it has the actions named GetEmployeeInfo, GetCompanyInfo and GetSalaryInfo. And I have defined the GetEmployeeInfo, GetCompanyInfo and not GetSalaryInfo.

public ActionResult GetEmployeeInfo()
{
    return View();
}
public ActionResult GetCompanyInfo()
{
    return View();
}

So while calling the GetSalaryInfo. it is giving me the above error.

In order to avoid this, we can use the "Controller.HandleUnknownAction". This method gets called when a controller cannot find an action method that matches a browser request. And in this action, we can customize our code by returning a view which will show the user a default message like "The action is an unknown action" or can log the Exception information or we can redirect to a different controller for further customized action. The defined functionality for this action can be like below:

protected override void HandleUnknownAction(string actionName)
    {
        this.View(errorpage).ExecuteResult(this.ControllerContext);
    }

And now, it will give me the below output instead of that error.

This is one way we can use the Controller.HandleUnknownAction. And there are other reasons we can use this function.

1. Method Overriding

Sometimes, we can find that there are so many actions that are not doing something besides calling the view with the same name like below:

public ActionResult GetEmployeeInfo()
{
    return View();
}
public ActionResult GetCompanyInfo()
{
    return View();
}

So by overriding the method HandleUnknownAction() of the Controller class, we are able to merge them all to reduce the unnecessary lines of code and simplify the action calls.

protected override void HandleUnknownAction(string actionName)
{
    this.View(actionName).ExecuteResult(this.ControllerContext);
}

2. Defining the Default Action

There might be a number of actions in the application which give the same result for the requested actions. In those cases, we can define one action with HandleUnknownAction and go ahead to give the output. We can also use switch case within this to define the various actions based on different conditions and a default action.

protected override void HandleUnknownAction(string actionName)
{
    switch (actionName)
    {
        case "GetEmployee":
            this.RedirectToAction("GetEmployeeInfo").ExecuteResult(this.ControllerContext);
            break;
        case "GetCompany":
            this.RedirectToAction("GetCompanyInfo").ExecuteResult(this.ControllerContext);
            break;
        default:
            this.RedirectToAction("HomePage").ExecuteResult(this.ControllerContext);
            break;               
    }
}

Points of Interest

So with the above points, we can use the "HandleUnknownAction" more efficiently to resolve some of our problems with the below mentioned advantages of this function:

  • Exception handling for unknown action request
  • Method overriding
  • Defining the default action

History

  • 21st October, 2014: Initial version

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here