如果是通过反射加载的控制器,那么视图文件将无法传递Model等数据,必须给视图文件声明视图文件所引用的程序集。
直接上代码。
/// <summary>
/// Razor视图页面的页面加添加动态引用程序集
/// </summary>
/// <param name="pluginName"></param>
RazorBuildProvider.CodeGenerationStarted += (object sender, EventArgs e) =>
{
RazorBuildProvider provider = (RazorBuildProvider)sender;
provider.AssemblyBuilder.AddAssemblyReference(视图文件所在的程序集); //Assembly
};
以上代码需要视图引擎中调用,实例代码:
public class CustomRazorViewEngine : RazorViewEngine
{
public override ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
{
CodeGeneration(”标识参数");
.....
}
public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
{
CodeGeneration(”标识参数");
...
}
//根据插件名称找出视图所在的程序集
private void CodeGeneration(string pluginName)
{
RazorBuildProvider.CodeGenerationStarted += (object sender, EventArgs e) =>
{
RazorBuildProvider provider = (RazorBuildProvider)sender;
var plugin = PluginManager.GetPlugin(pluginName);
if (plugin != null)
{
provider.AssemblyBuilder.AddAssemblyReference(plugin.Assembly);
}
};
}