动态加载的程序集中的强类型Razor视图在运行时不会编译 [英] Strongly typed Razor views in dynamically loaded assembly don't compile at runtime

查看:184
本文介绍了动态加载的程序集中的强类型Razor视图在运行时不会编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在装配体中有一个MVC3区域,该装配体被动态加载了MEF.
如果我将Webforms视图引擎与强类型视图一起使用,则一切正常.
如果我将Razor视图引擎与dynamic模型一起使用,它也可以正常工作.

I have a MVC3 Area in an assembly that is dynamically loaded with MEF.
If I use the Webforms view engine with strongly typed views, everything works fine.
If I use the Razor view engine with a dynamic model, it works fine too.

但是,如果我使用强类型的Razor视图,则视图编译会在运行时失败.

But if I use a strongly typed Razor view, the view compilation fail at runtime.

问题是动态加载的程序集不在传递给C#编译器的引用程序集列表中.

The problem is that the dynamically loaded assembly is not in the list of referenced assemblies passed to the C# compiler.

生成的C#代码如下:

namespace ASP {
    using System;
    /* Other namespaces */
    public class _Page_MyApp_Views_Home_Index_cshtml
        : System.Web.Mvc.WebViewPage<MyApp.ViewModels.Search.IndexViewModel> {
    /* Generated code */
    }
}

这是错误消息:

Compiler Error Message: CS0246: The type or namespace name 'MyApp' could not be found (are you missing a using directive or an assembly reference?)

您知道为什么它可以与Webforms视图引擎一起使用而不与Razor一起使用吗? 有没有办法告诉编译器使用我的动态加载程序集进行编译?

Do you have any idea why this works with the Webforms view engine but not with Razor ? Is there a way to tell the compiler to use my dynamically loaded assembly for the compilation ?

谢谢

推荐答案

当由于无法找到强类型视图而导致编译失败时,我遇到了类似的问题. 一种解决方案是创建从RazorBuildProvider继承的自定义构建提供程序:

I had the similar problem, when compilation of strongly typed views fails due to not being able to find the type. One solution is to create custom build provider that inherits from RazorBuildProvider:

public class CustomRazorBuildProvider : RazorBuildProvider
{
  public override void GenerateCode(System.Web.Compilation.AssemblyBuilder assemblyBuilder) 
  {
    Assembly a = Assembly.LoadFrom([PATH_TO_YOUR_ASSEMBLY]);
    assemblyBuilder.AddAssemblyReference(a);      
    base.GenerateCode(assemblyBuilder);
  }

}

然后,您需要在web.config中注册此自定义生成提供程序:

Then you need to register this custom build provider in web.config:

<compilation debug="true" targetFramework="4.0">
  <assemblies>
  ...
  </assemblies>
  <buildProviders>
    <remove extension=".cshtml" />
    <add extension=".cshtml" type="YouAssembly.CustomRazorBuildProvider, YourAssembly"/>
  </buildProviders>
</compilation>

它可以工作,但是不好的是,每次编译视图时都需要引用程序集.

It works, but the bad thing is that assemblies need to be referenced everytime the view is compiled.

此处,我已经发布了一个更好的解决方案的问题(例如类似于AppDomain.CurrentDomain.AddPrivatePath),在该问题中,您只需指定一次私有路径,并且它将在目录级别,而不是程序集级别.

Here, I have posted a question for a nicer solution (for example similar to AppDomain.CurrentDomain.AddPrivatePath) where you would just specify private path once, and it would be on directory level, not on assembly level.

这篇关于动态加载的程序集中的强类型Razor视图在运行时不会编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆