Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set a "Default Action" for my Controller that will be called when no other action matches?

Tags:

asp.net-mvc

Say I have the following route:

routes.MapRoute("Default", "{controller}/{action}/{id}", 
    new { controller = "Home", action = "Index", id = "" });

Lets also say that my controller has the following methods: Index(Int32 id) and Edit(Int32 id).

So /MyController/Index/1 is a valid URL for that route. So is /MyController/Edit/1

However, if a URL is received that correctly maps to my controller but not to an existing action, how do I define a "Default Action" to execute instead of letting the MVC framework throw up an error screen?

Basically I'd like the URLs /MyController/Preview/1 and /MyController/Whatever/1 to execute an action that I specify ahead of time when the {action} token can't be mapped to an existing action on my controller.

I see that the MvcContrib project on Codeplex has an attribute that enables this for use with the ConventionController, but I'd like to keep this with pure MS ASP.NET MVC for now.

I also see that Fredrik mentions a [ControllerAction(DefaultAction = true)] attribute, but I can't find mention of it anywhere except his blog (and my app won't compile when I try it in my controller).

like image 486
Doug Wilson Avatar asked Nov 18 '08 22:11

Doug Wilson


People also ask

What is the default controller?

Defining a Default ControllerCodeIgniter can be told to load a default controller when a URI is not present, as will be the case when only your site root URL is requested. To specify a default controller, open your application/config/routes. php file and set this variable: $route['default_controller'] = ' Blog ';

What is Actionresult () in MVC?

An action result is what a controller action returns in response to a browser request. The ASP.NET MVC framework supports several types of action results including: ViewResult - Represents HTML and markup. EmptyResult - Represents no result.

What is an action method?

An action method is a static method that takes some parameters as input and always returns an instance of (a subclass of) the Result class. In other words, an action method always takes the following form: public static play. mvc. Result methodName(params...)


2 Answers

You can do the following for now.

protected override void HandleUnknownAction(string actionName) {
  //your code here.
}

Another approach is that you put a constraint on the default route so it only matches methods you know exist on the controller. Then you could have another route like so:

routes.MapRoute("default-action", "{controller}/{actionName}/{id}", new {action="DefaultAction"});

Which maps to

public ActionResult DefaultAction(string actionName, string id) {
  //handle default action
}

This gets you the result you're looking for.

like image 152
Haacked Avatar answered Sep 18 '22 19:09

Haacked


Farooq Kaiser did an article on CodeProject on this topic which I found useful: Handling Unknown Actions in ASP.NET MVC

I particularly like the trick of creating "view only" pages (obviously error handling code should be added):

protected override void HandleUnknownAction(string actionName)
{
   this.View(actionName).ExecuteResult(ControllerContext);
}
like image 41
Matthew Nichols Avatar answered Sep 17 '22 19:09

Matthew Nichols