在我的一个控制器+动作对中,我从另一个地方获取另一个控制器和动作的值作为字符串,我想重定向我当前的动作.在进行重定向之前,我想确保我的应用程序中存在控制器+操作,如果没有,则重定向到404.我正在寻找一种方法来执行此操作.
public ActionResult MyTestAction()
{
string controller = getFromSomewhere();
string action = getFromSomewhereToo();
/*
At this point use reflection and make sure action and controller exists
else redirect to error 404
*/
return RedirectToRoute(new { action = action, controller = controller });
}
Run Code Online (Sandbox Code Playgroud)
我所做的就是这个,但它不起作用.
var cont = Assembly.GetExecutingAssembly().GetType(controller);
if (cont != null && cont.GetMethod(action) != null)
{
// controller and action pair is valid
}
else
{
// controller and action pair is invalid
}
Run Code Online (Sandbox Code Playgroud)