原理,在一个Action里面return 另一个Action出去。
public class HomeController : Controller { // GET: Home public ActionResult Index(int? id) { //必须要把Route里的Action改成最终的Action名字,否则造成读取CSHTML错误 ControllerContext.RouteData.Values["Action"] = "TS"; return TS(id); //如果不确定到哪个Action,可以使用反射的方式来跳转 //return this.GetType().GetMethod("TS").Invoke(this,new object[]{id}) as ActionResult; } public ActionResult TS(int? id) { ViewBag.Id = id; return View(); } }
如果连控制器也不确定,可以这么写
ControllerContext.RouteData.Values["Action"] = "Index"; ControllerContext.RouteData.Values["Controller"] = "Test"; Type type = Assembly.GetExecutingAssembly().DefinedTypes.Where(t => t.Name == "TestController").FirstOrDefault(); object obj = Assembly.GetExecutingAssembly().CreateInstance(type.FullName); return obj.GetType().GetMethod("Index").Invoke(obj, null) as ActionResult;
时间: 2024-10-12 20:08:25