What's an equivalent of ASP.NET MVC 5
Controller.HandleUnknownAction()
in ASP.NET MVC 6 / ASP.NET 5?
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"});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With