c# - 当引用构建为控制台应用程序的程序集时,MVC View get 编译错误 (CS0246)。当它构建为类库时 - 它工作正常

标签 c# asp.net-mvc razor asp.net-mvc-5.1

我有一个解决方案。第一个项目包含 Entity Framework 模型,输出类型是控制台应用程序。其他项目是前端,asp.net mvc 5 应用程序,具有第一个项目中实体的 CRUD View 。第一个项目从第二个项目引用,我还在 web.config 中添加了依赖程序集:

<dependentAssembly>
 <assemblyIdentity name="VolLoader" publicKeyToken="null" />
</dependentAssembly>

但是当我访问使用此程序集中的实体的 View 时,我收到 View 编译错误:

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

Source Error:



Line 29:     
Line 30:     
Line 31:     public class _Page_Views_JobMon_Index_cshtml : System.Web.Mvc.WebViewPage<IEnumerable<VolLoader.Data.JobToWatch>> {
Line 32:         
Line 33: #line hidden

在views文件夹中的web.config中添加namespace()会导致每个 View 出现编译错误。

程序集被复制到输出。

但是当我将第一个项目的输出类型更改为类库时,它可以工作,没有编译错误。我不明白为什么?有人有想法吗?

最佳答案

我想我找到了为什么会发生这种情况。我不是100%确定,但我无法重新编译system.web.dll来证明这一点。 我在system.web.dll中找到了这段代码:

namespace System.Web.Compilation
{
    /// <summary>Provides a container for building an assembly from one or more virtual paths within an ASP.NET project.</summary>
    public class AssemblyBuilder
......
}

第 381 行:

internal CompilerParameters GetCompilerParameters()
{
    CompilerParameters compilerParameters = this._compilerType.CompilerParameters;
    string text = this._tempFiles.TempDir;
    if (this.CultureName != null)
    {
        text = Path.Combine(text, this.CultureName);
        Directory.CreateDirectory(text);
        compilerParameters.OutputAssembly = Path.Combine(text, this.OutputAssemblyName + ".resources.dll");
    }
    else
    {
        compilerParameters.OutputAssembly = Path.Combine(text, this.OutputAssemblyName + ".dll");
    }

如您所见,它只需要 dll:

compilerParameters.OutputAssembly = Path.Combine(text, this.OutputAssemblyName + ".dll");

关于c# - 当引用构建为控制台应用程序的程序集时,MVC View get 编译错误 (CS0246)。当它构建为类库时 - 它工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21616799/

相关文章:

c# - 在 ServiceStack json 中返回空值

c# - 带有 List<> 项目的玻璃映射器 Editable() 不工作?

asp.net-mvc - 如何为 ASP.NET MVC 站点定义一个包罗万象的路由?

javascript - 根据选择重新加载面板中的不同 HTML 控件

javascript - 我必须编写正则表达式,只允许 @#$% 作为特殊符号,而不允许其他符号,例如 ?><`~ 符号

c# - 如何使用 DirectoryInfo.GetFiles 并在找到第一个匹配项后停止?

asp.net-mvc - 我应该如何实现 Nlog 记录器以减少复制/粘贴的单调乏味?

asp.net-mvc - 需要为基于 Razor 的 View 编写单元测试的指针

asp.net-mvc-3 - Razor 语法没有按我预期的方式工作

css - 仅仅为了那个 View 而切换CSS?