从 ASP.NET Core View 引用 NET461 DLL 模型类型

Reference a NET461 DLL model type from ASP.NET Core View

我有一个新的 ASP.NET Core RC2 构建如下:

当引用 DLL 库中的模型类型和 运行 项目时,出现以下错误:

An error occurred during the compilation of a resource required to process this request. Please review the following specific error details and modify your source code appropriately. Generated Code The type or namespace name 'Data' does not exist in the namespace 'ADMA.EWRS' (are you missing an assembly reference?) 23. public class _Views_Murad_Index_cshtml : Microsoft.AspNetCore.Mvc.Razor.RazorPage> The type or namespace name 'Data' does not exist in the namespace 'ADMA.EWRS' (are you missing an assembly reference?) 39. public Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper> Html { get; private set; } An error occurred during the compilation of a resource required to process this request. Please review the following specific error details and modify your source code appropriately. Generated Code The type or namespace name 'Data' does not exist in the namespace 'ADMA.EWRS' (are you missing an assembly reference?) 23. public class _Views_Murad_Index_cshtml : Microsoft.AspNetCore.Mvc.Razor.RazorPage> The type or namespace name 'Data' does not exist in the namespace 'ADMA.EWRS' (are you missing an assembly reference?) 39. public Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper> Html { get; private set; }

配置Project.json如下:

{
  "dependencies": {
    "Microsoft.ApplicationInsights.AspNetCore": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Diagnostics": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview1-final",
      "type": "build"
    },
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging.Console": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging.Debug": "1.0.0-rc2-final",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc2-final",
    "Microsoft.AspNetCore.Authorization": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Authentication": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Authentication.Cookies": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Session": "1.0.0-rc2-final",
    "ADMA.EWRS.Web.Security": "1.0.0-*"
  },

  "tools": {
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": "portable-net45+win8+dnxcore50"
    },
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": "portable-net45+win8+dnxcore50"
    }
  },

  "frameworks": {
    "net461": {
      "dependencies": {
        "ADMA.EWRS.BizDomain": {
          "target": "project"
        },
        "ADMA.EWRS.Data.Models": {
          "target": "project"
        }
      }
    }
  },


  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "Views",
      "appsettings.json",
      "web.config"
    ]
  },

  "scripts": {
    "prepublish": [ "npm install", "bower install", "gulp clean", "gulp min" ],
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

Solution Explorer

X Project

添加 Razor 选项后解决,如下代码所示:

 services.AddMvc().// Murad Add this for RC2, remove it if release 1.0 after June 
                AddRazorOptions(options =>
            {
                var previous = options.CompilationCallback;
                options.CompilationCallback = context =>
                {
                    previous?.Invoke(context);
                    context.Compilation = context.Compilation.AddReferences(Microsoft.CodeAnalysis.MetadataReference.CreateFromFile(typeof(ADMA.EWRS.Data.Models.Murad).Assembly.Location));
                };
            });

            //var myAssemblies = AppDomain.CurrentDomain.GetAssemblies().Select(x => Microsoft.CodeAnalysis.MetadataReference.CreateFromFile(x.Location)).ToList();
            //services.Configure((Microsoft.AspNetCore.Mvc.Razor.RazorViewEngineOptions options) =>
            //{
            //    var previous = options.CompilationCallback;
            //    options.CompilationCallback = (context) =>
            //    {
            //        previous?.Invoke(context);

            //        context.Compilation = context.Compilation.AddReferences(myAssemblies);
            //    };
            //});

勾选

https://github.com/aspnet/Mvc/issues/4686