.net核心,共享视图和控制器,从.net Framework项目加载视图

时间:2018-10-18 16:03:22

标签: c# asp.net asp.net-mvc razor asp.net-core

我有.net标准2.0库(sdk = Microsoft.NET.Sdk.Razor),称为SelfHost。 SelfHost使用MVC启动Kestrel Web主机服务器。 SelfHost还包含控制器和视图。构建生成SelfHost.Views.dll,从SelfHost加载控制器,一切正常。棘手的部分是:我希望SlefHost还可以从客户端应用程序(引用此库的程序集)中加载控制器和视图。 SelfHost使用以下命令从另一个程序集中检测控制器:

services.AddApplicationPart(clientAssembly)

从另一个程序集中检测视图是通过以下方式实现的:

services.Configure<RazorViewEngineOptions>(options => options.FileProviders.Add(new EmbeddedFileProvider(clientAssembly));

在这种方法中,视图构建动作必须设置为“嵌入式资源”

案例1:

运行使用SelfHost的.net核心控制台应用程序(sdk = Microsoft.NET.Sdk),而无需向控制台应用程序添加其他控制器和视图。因此,我们拥有仅带有SelfHost控制器和视图的干净控制台应用程序。

结果1 : 一切正常。我有权访问控制器,并返回视图。

案例2:  运行使用SelfHost的.net核心控制台应用程序(sdk = Microsoft.NET.Sdk。 Razor ),配合向控制台应用程序添加其他控制器和视图。因此,我们拥有控制台应用程序,其中包含来自自托管主机的控制器/视图和来自控制台应用程序的控制器/视图。

结果2 一切正常。我可以访问SelfHost和控制台应用程序控制器。从SelfHost和控制台应用程序返回的视图。

情况3: 运行使用SelfHost的.net核心控制台应用程序(sdk = Microsoft.NET.Sdk,不带Razor),使用向控制台应用程序添加其他控制器和视图。因此,我们拥有控制台应用程序,其中包含来自自托管主机的控制器/视图和来自控制台应用程序的控制器/视图。

结果3:

当尝试从控制台应用程序访问视图时,我得到:

One or more compilation references are missing. Ensure that your project is referencing 'Microsoft.NET.Sdk.Web' and the 'PreserveCompilationContext' property is not set to false.

The type or namespace name 'Microsoft' could not be found in the global namespace (are you missing an assembly reference?)
+
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(AspNetCore._Views_Jakis_Index), @"mvc.1.0.view", @"/Views/Jakis/Index.cshtml")]
The type or namespace name 'Microsoft' could not be found in the global namespace (are you missing an assembly reference?)
+
[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@"/Views/Jakis/Index.cshtml", typeof(AspNetCore._Views_Jakis_Index))]
Predefined type 'System.Type' is not defined or imported
+
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(AspNetCore._Views_Jakis_Index), @"mvc.1.0.view", @"/Views/Jakis/Index.cshtml")]
Predefined type 'System.String' is not defined or imported
+
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(AspNetCore._Views_Jakis_Index), @"mvc.1.0.view", @"/Views/Jakis/Index.cshtml")]
Predefined type 'System.String' is not defined or imported
+
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(AspNetCore._Views_Jakis_Index), @"mvc.1.0.view", @"/Views/Jakis/Index.cshtml")]
Predefined type 'System.String' is not defined or imported
+
[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@"/Views/Jakis/Index.cshtml", typeof(AspNetCore._Views_Jakis_Index))]
Predefined type 'System.Type' is not defined or imported
+
[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@"/Views/Jakis/Index.cshtml", typeof(AspNetCore._Views_Jakis_Index))]
The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
+
    using System;
The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
+
    using System.Collections.Generic;
The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
+
    using System.Linq;
The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
+
    using System.Threading.Tasks;
The type or namespace name 'Microsoft' could not be found (are you missing a using directive or an assembly reference?)
+
    using Microsoft.AspNetCore.Mvc;
The type or namespace name 'Microsoft' could not be found (are you missing a using directive or an assembly reference?)
+
    using Microsoft.AspNetCore.Mvc.Rendering;
The type or namespace name 'Microsoft' could not be found (are you missing a using directive or an assembly reference?)
+
    using Microsoft.AspNetCore.Mvc.ViewFeatures;
The type or namespace name 'Microsoft' could not be found in the global namespace (are you missing an assembly reference?)
+
    [global::Microsoft.AspNetCore.Razor.Hosting.RazorSourceChecksumAttribute(@"SHA1", @"3884152d0999a8c1c979e05ec6b3e584997f3684", @"/Views/Jakis/Index.cshtml")]
Predefined type 'System.String' is not defined or imported
+
    [global::Microsoft.AspNetCore.Razor.Hosting.RazorSourceChecksumAttribute(@"SHA1", @"3884152d0999a8c1c979e05ec6b3e584997f3684", @"/Views/Jakis/Index.cshtml")]
Predefined type 'System.String' is not defined or imported
+
    [global::Microsoft.AspNetCore.Razor.Hosting.RazorSourceChecksumAttribute(@"SHA1", @"3884152d0999a8c1c979e05ec6b3e584997f3684", @"/Views/Jakis/Index.cshtml")]
Predefined type 'System.String' is not defined or imported
+
    [global::Microsoft.AspNetCore.Razor.Hosting.RazorSourceChecksumAttribute(@"SHA1", @"3884152d0999a8c1c979e05ec6b3e584997f3684", @"/Views/Jakis/Index.cshtml")]
The type or namespace name 'Microsoft' could not be found in the global namespace (are you missing an assembly reference?)
+
    public class _Views_Jakis_Index : global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<dynamic>
Cannot define a class or member that utilizes 'dynamic' because the compiler required type 'System.Runtime.CompilerServices.DynamicAttribute' cannot be found. Are you missing a reference?
+
    public class _Views_Jakis_Index : global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<dynamic>
Predefined type 'System.Boolean' is not defined or imported
+
    public class _Views_Jakis_Index : global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<dynamic>
The return type of an async method must be void, Task or Task<T>
+
        public async override global::System.Threading.Tasks.Task ExecuteAsync()
The type or namespace name 'System' could not be found in the global namespace (are you missing an assembly reference?)
+
        public async override global::System.Threading.Tasks.Task ExecuteAsync()
The type or namespace name 'Microsoft' could not be found in the global namespace (are you missing an assembly reference?)
+
        public global::Microsoft.AspNetCore.Mvc.ViewFeatures.IModelExpressionProvider ModelExpressionProvider { get; private set; }
Predefined type 'System.Void' is not defined or imported
+
        public global::Microsoft.AspNetCore.Mvc.ViewFeatures.IModelExpressionProvider ModelExpressionProvider { get; private set; }
The type or namespace name 'Microsoft' could not be found in the global namespace (are you missing an assembly reference?)
+
        public global::Microsoft.AspNetCore.Mvc.IUrlHelper Url { get; private set; }
Predefined type 'System.Void' is not defined or imported
+
        public global::Microsoft.AspNetCore.Mvc.IUrlHelper Url { get; private set; }
The type or namespace name 'Microsoft' could not be found in the global namespace (are you missing an assembly reference?)
+
        public global::Microsoft.AspNetCore.Mvc.IViewComponentHelper Component { get; private set; }
Predefined type 'System.Void' is not defined or imported
+
        public global::Microsoft.AspNetCore.Mvc.IViewComponentHelper Component { get; private set; }
The type or namespace name 'Microsoft' could not be found in the global namespace (are you missing an assembly reference?)
+
        public global::Microsoft.AspNetCore.Mvc.Rendering.IJsonHelper Json { get; private set; }
Predefined type 'System.Void' is not defined or imported
+
        public global::Microsoft.AspNetCore.Mvc.Rendering.IJsonHelper Json { get; private set; }
The type or namespace name 'Microsoft' could not be found in the global namespace (are you missing an assembly reference?)
+
        public global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<dynamic> Html { get; private set; }
Predefined type 'System.Object' is not defined or imported
+
        public global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<dynamic> Html { get; private set; }
Cannot define a class or member that utilizes 'dynamic' because the compiler required type 'System.Runtime.CompilerServices.DynamicAttribute' cannot be found. Are you missing a reference?
+
        public global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<dynamic> Html { get; private set; }
Predefined type 'System.Boolean' is not defined or imported
+
        public global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<dynamic> Html { get; private set; }
Predefined type 'System.Void' is not defined or imported
+
        public global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<dynamic> Html { get; private set; }
'_Views_Jakis_Index.ExecuteAsync()': no suitable method found to override
+
        public async override global::System.Threading.Tasks.Task ExecuteAsync()
The type or namespace name 'Microsoft' could not be found in the global namespace (are you missing an assembly reference?)
+
        [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
The type or namespace name 'Microsoft' could not be found in the global namespace (are you missing an assembly reference?)
+
        [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
The type or namespace name 'Microsoft' could not be found in the global namespace (are you missing an assembly reference?)
+
        [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]

当控制台应用程序位于.net框架堆栈上时,也会发生此错误。 我想这是因为没有可用的razor sdk来编译此视图。 有什么解决办法吗?我想在.net框架组件中也使用此SelfHost库,在该组件中我无法将sdk设置为Microsoft.NET.Sdk.Razor。

0 个答案:

没有答案