相关疑难解决方法(0)

在重定向,asp.net mvc3之前,如何确保控制器和操作存在

在我的一个控制器+动作对中,我从另一个地方获取另一个控制器和动作的值作为字符串,我想重定向我当前的动作.在进行重定向之前,我想确保我的应用程序中存在控制器+操作,如果没有,则重定向到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)

c# reflection asp.net-mvc asp.net-mvc-3

11
推荐指数
2
解决办法
8165
查看次数

标签 统计

asp.net-mvc ×1

asp.net-mvc-3 ×1

c# ×1

reflection ×1