Click here to Skip to main content
Licence CPOL
First Posted 16 Aug 2009
Views 8,170
Bookmarked 10 times

Handling Unknown Actions in ASP.NET MVC

By | 16 Aug 2009 | Article
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)

About the Author

Farooq Kaiser

Software Developer (Senior)
http://www.Fairnet.com
Canada Canada

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralGreat tip PinmemberBrian Lowe2:12 18 Aug '09  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 16 Aug 2009
Article Copyright 2009 by Farooq Kaiser
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid