Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HandleUnknownAction in ASP.NET 5

What's an equivalent of ASP.NET MVC 5

Controller.HandleUnknownAction() 

in ASP.NET MVC 6 / ASP.NET 5?

like image 494
Mikeon Avatar asked May 04 '15 13:05

Mikeon


2 Answers

There's no real equivalent.

Action Selection in MVC5/WebAPI2 was a three stage process: 1. Run the routes 2. Select a controller 3. Select an action

In MVC6, step 2 is gone. Actions are selected directly using route values - you'll notice that Controller.BeginExecute is gone as well. Controllers are 'thin' now.

You can simulate this behavior if you want by using a route that goes directly to your action in question.

Define an action called HandleUnknownAction in your controller routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}"); routes.MapRoute("unknown_action", "{controller}/{*params}", defaults: new { action = "HandleUnknownAction"});

like image 119
Ryan Nowak Avatar answered Nov 07 '22 17:11

Ryan Nowak


An alternative approach is to simply define the unknown action as a parameter of your route:

[Route("[controller]")]
public class FooController : Controller
{

    [HttpGet("{viewName}")]
    public IActionResult HandleUnknownAction(string viewName)
    {
        return View(viewName);

    }
}

Using this approach, the url foo/bar would return the View bar.cshtml, foo/baz would return baz.cshtml etc.

like image 1
Party Ark Avatar answered Nov 07 '22 19:11

Party Ark