Click here to Skip to main content
14,974,250 members
Articles / Web Development / XHTML
Article
Posted 16 Aug 2009

Stats

33.1K views
17 bookmarked

Handling Unknown Actions in ASP.NET MVC

Rate me:
Please Sign up or sign in to vote.
5.00/5 (5 votes)
16 Aug 2009CPOL1 min read
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#:

C#
protected virtual void HandleUnknownAction(string actionName) 

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

C#
[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:

Image 1

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
No Biography provided

Comments and Discussions

 
GeneralGreat tip Pin
Brian Lowe18-Aug-09 2:12
MemberBrian Lowe18-Aug-09 2:12 

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

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