首页 > Web开发 > 详细

MVC不用302跳转Action,内部跳转

时间:2015-03-19 19:49:42      阅读:415      评论:0      收藏:0      [点我收藏+]

原理,在一个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;

 

MVC不用302跳转Action,内部跳转

原文:http://www.cnblogs.com/xdoudou/p/4351140.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!