According to the inlined documentation, ControllerBase.RedirectToAction
takes both the action name and the controller name:
// Parameters:
// actionName:
// The name of the action.
//
// controllerName:
// The name of the controller.
public virtual RedirectToActionResult RedirectToAction(string actionName, string controllerName);
Now, let's assume I want to redirect to the following action:
[Route("Whatever")]
public class WhateverController : Controller
{
[HttpGet("Overview")]
public IActionResult Overview()
{
return View();
}
}
Naturally, I wanted to use the nameof
operator:
[Route("Home")]
public class HomeController : Controller
{
[HttpGet("Something")]
public IActionResult Something()
{
return RedirectToAction(
nameof(WhateverController.Overview), // action name
nameof(WhateverController) // controller name
);
}
}
But that call fails with the error InvalidOperationException: No route matches the supplied values.
I know I could hardcode the controller name to "whatever" instead of using the nameof
operator, but is there a way to get the proper name from the class name?