Click here to Skip to main content
Click here to Skip to main content
Go to top

Handling Unknown Actions in ASP.NET MVC

, 16 Aug 2009
Rate this:
Please Sign up or sign in to vote.
In this article, I will explore handling unknown actions. A Controller.HandleUnknownAction method gets called when a controller cannot find an action method that matches a browser request.

Introduction

A Controller.HandleUnknownAction method gets called when a controller cannot find an action method that matches a browser request.

Background

In this article, I will explore handling unknown actions. A Controller.HandleUnknownAction method gets called when a controller cannot find an action method that matches a browser request. I have implemented the following method in my previous article: Implementing HTTP File Upload with ASP.NET MVC.

Here is the syntax in C#:

protected virtual void HandleUnknownAction(string actionName) 

Here is a FileUploadController class that implements HandleUnknownAction as shown below:

[HandleError]
public class FileUploadController : Controller 
{ 
    public ActionResult FileUpload() 
    {
        return View(); 
    } 
    [AcceptVerbs(HttpVerbs.Post)] 
    public ActionResult FileUpload(HttpPostedFileBase uploadFile) 
    { 
        if (uploadFile.ContentLength > 0) 
        { 
            string filePath = 
               Path.Combine(HttpContext.Server.MapPath("../Uploads"), 
                            Path.GetFileName(uploadFile.FileName)); 
            uploadFile.SaveAs(filePath); 
        } 
        return View(); 
    } 
    protected override void HandleUnknownAction(string actionName) 
    { 
        actionName = "FileUpload";
        this.View(actionName).ExecuteResult(this.ControllerContext); 
    } 
}

The above example displays the FileUpload view when a request for the FileUpload action is made on the controller. If there is no matching view, then the FileUpload controller HandleUnknownAction() method is invoked. I have hard coded the FileUpload view so that if the browser request does not match the FileUpload, it will explicitly call the FileUpload action. Here is the view with the unknown action:

Notice that the controller does not have the UploadToGoogle action, but our HandleUnknownAction() method is invoked and it explicitly calls the FileUpload action.

Summary

A Controller.HandleUnknownAction method gets called when a controller cannot find an action method that matches a browser request. Therefore, in your controller class, you don’t need to explicitly code an action method for each view.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Farooq Kaiser
Software Developer (Senior) http://www.Fairnet.com
Canada Canada
12+ years of complete software development life cycle experience for web based applications and multi-tier client-server desktop, primarily using LINQ, WCF, WWF, C#, ASP.NET, XML, XSLT, AJAX, Winforms,Visual Basic, JavaScript, JQuery, Google APIs, C++, VB.NET, C, ATL/COM, Open XML. Extensively involved in the requirement analysis, feasibility study, conceptualization, planning, architecture/design, configuration, development, quality assurance, implementation and release of the software products.

Comments and Discussions

 
GeneralGreat tip PinmemberBrian Lowe18-Aug-09 2:12 
This is an excellent tip. I can see it's use in a few controllers to spit a default action in the case of the user trying to spoof the URL (after logging the fail, of course).
 
I'm stuck now on how to redirect to a different controller. For example, in the Account controller there isn't an Index action, and there isn't a suitable action to default to either, so I'd like to show the /Home/Index action instead.
 
The HandleUnknownAction function is void, but I need to finish up with
return RedirectToAction("Index", "Home");
 
In your code you use
this.View("Index").ExecuteResult(this.ControllerContext);
but how can I get the ControllerContext to be one other than 'this'?

 
Brian
----@

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 16 Aug 2009
Article Copyright 2009 by Farooq Kaiser
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid