asp.net mvc在一个控制器中返回另外一个控制器的视图



调用其他控制器返回result:


   ReflectedControllerDescriptor RefControllerDescriptor = new ReflectedControllerDescriptor(typeof(AnotherController));
           
            ActionDescriptor Actescriptor = RefControllerDescriptor.FindAction(RefControllerContext, "ViewNme");
              ViewResult obj = Actescriptor.Execute(RefControllerContext, new Dictionary<string, object>() { }) as ViewResult;
           return obj;
调用其他控制器返回result,使用当前控制器的viewdata,Model:


 ControllerContext RefControllerContext = new ControllerContext(ControllerContext.RequestContext, new AnotherController());
                ViewEngineResult ve = ViewEngines.Engines.FindView(RefControllerContext, "ViewNme", null);


                return View(ve.View, tp.Item2);

正确使用方法:

using System;
using System.Linq;
using System.Collections.Generic;

namespace System.Web.Mvc
{
    public class ReflectedRazorViewEngine : RazorViewEngine
    {
        public ReflectedRazorViewEngine()
        {
        }
       public  static ReflectedRazorViewEngine Current= new ReflectedRazorViewEngine();


     
        /// <summary>
        /// 获取指定路径视图
        /// </summary>
        /// <param name="controllerContext"></param>
        /// <param name="ViewName"></param>
        /// <param name="ControllerName"></param>
        /// <param name="AreaName"></param>
        /// <param name="MasterPath"></param>
        /// <returns></returns>
       public IView ReflectedViewWithArea(ControllerContext controllerContext, string ViewName, string ControllerName, string AreaName, string MasterPath = null)
        {
            
            if (controllerContext == null)
            {
                throw new ArgumentNullException("控制器对象获取识别");
            }
            if (string.IsNullOrEmpty(ViewName))
            {
                throw new ArgumentException("无法找到视图文件");
            }

           //循环待area的视图模版
           foreach (string path in AreaViewLocationFormats.Select(h => string.Format(h, ViewName, ControllerName, AreaName)))
            {
             //默认存在判断
                if (this.FileExists(controllerContext,path))
                {
                    //创建视图传参数
                    return CreateView(controllerContext, path, MasterPath);

                }
            }
           
            return null;
        } 
        public  IView ReflectedView(ControllerContext controllerContext, string ControllerName, string ViewName,string MasterPath=null)
        {
            
            if (controllerContext == null)
            {
                throw new ArgumentNullException("controllerContext");
            }
            if (string.IsNullOrEmpty(ViewName))
            {
                throw new ArgumentException("NullOrEmptyviewName");
            }

            foreach (string path in ViewLocationFormats.Select(h => string.Format(h, ViewName, ControllerName)))
            {

                if (this.FileExists(controllerContext, path))
                {


                    return CreateView(controllerContext, path, MasterPath);

                }
            }
            return null;
        }

       
    }
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值