asp.Netmvcは1つのコントローラで別のコントローラのビューを返します

3215 ワード

他のコントローラを呼び出してresultを返します.
   ReflectedControllerDescriptor RefControllerDescriptor = new ReflectedControllerDescriptor(typeof(AnotherController));
           
            ActionDescriptor Actescriptor = RefControllerDescriptor.FindAction(RefControllerContext, "ViewNme");
              ViewResult obj = Actescriptor.Execute(RefControllerContext, new Dictionary() { }) 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();


     
        /// 
        ///         
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
       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;
        }

       
    }
}