调用其他控制器返回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;
}
}
}