Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Razor 如何在ASP.NET核心MVC中重用视图(页面)?_Razor_Asp.net Core_Reusability - Fatal编程技术网

Razor 如何在ASP.NET核心MVC中重用视图(页面)?

Razor 如何在ASP.NET核心MVC中重用视图(页面)?,razor,asp.net-core,reusability,Razor,Asp.net Core,Reusability,在ASP.NET Core MVC之前,我们将使用RazorGenerator将视图编译成程序集,并在另一个项目中重用这些视图,方法是引入一个自定义视图引擎,该引擎将从程序集而不是文件系统加载视图 // using System.Reflection; // using Microsoft.AspNetCore.Mvc.ApplicationParts; // using Microsoft.AspNetCore.Mvc.ViewComponents; public void Configur

在ASP.NET Core MVC之前,我们将使用RazorGenerator将视图编译成程序集,并在另一个项目中重用这些视图,方法是引入一个自定义视图引擎,该引擎将从程序集而不是文件系统加载视图

// using System.Reflection;
// using Microsoft.AspNetCore.Mvc.ApplicationParts;
// using Microsoft.AspNetCore.Mvc.ViewComponents;

public void ConfigureServices(IServiceCollection services)
{
     ...
     var assembly = typeof(ClassInYourLibrary).GetTypeInfo().Assembly;
     var part = new AssemblyPart(assembly);

     services.AddMvc()
             .ConfigureApplicationPartManager(p => {
                p.ApplicationParts.Add(part);
                p.FeatureProviders.Add(new ViewComponentFeatureProvider());
             });
}
在ASP.NET Core MVC中,预编译视图的概念是,它适用于2.0版,并创建一个程序集,按照惯例,该程序集的名称为project_name.PrecompiledViews.dll

// using System.Reflection;
// using Microsoft.AspNetCore.Mvc.ApplicationParts;
// using Microsoft.AspNetCore.Mvc.ViewComponents;

public void ConfigureServices(IServiceCollection services)
{
     ...
     var assembly = typeof(ClassInYourLibrary).GetTypeInfo().Assembly;
     var part = new AssemblyPart(assembly);

     services.AddMvc()
             .ConfigureApplicationPartManager(p => {
                p.ApplicationParts.Add(part);
                p.FeatureProviders.Add(new ViewComponentFeatureProvider());
             });
}
我有两个问题,虽然我找不到一个关于谷歌的答案。 首先,我不知道如何在另一个项目中重用该DLL。就像我在
CompanyBase.dll
中有
About.cshtml
页面一样,我如何在
ProjectAlpha
中重用该页面/视图

// using System.Reflection;
// using Microsoft.AspNetCore.Mvc.ApplicationParts;
// using Microsoft.AspNetCore.Mvc.ViewComponents;

public void ConfigureServices(IServiceCollection services)
{
     ...
     var assembly = typeof(ClassInYourLibrary).GetTypeInfo().Assembly;
     var part = new AssemblyPart(assembly);

     services.AddMvc()
             .ConfigureApplicationPartManager(p => {
                p.ApplicationParts.Add(part);
                p.FeatureProviders.Add(new ViewComponentFeatureProvider());
             });
}
而且我不希望视图编译在发布时发生。如何将其更改为在构建时发生?

ASP.NET Core中有一个概念:

// using System.Reflection;
// using Microsoft.AspNetCore.Mvc.ApplicationParts;
// using Microsoft.AspNetCore.Mvc.ViewComponents;

public void ConfigureServices(IServiceCollection services)
{
     ...
     var assembly = typeof(ClassInYourLibrary).GetTypeInfo().Assembly;
     var part = new AssemblyPart(assembly);

     services.AddMvc()
             .ConfigureApplicationPartManager(p => {
                p.ApplicationParts.Add(part);
                p.FeatureProviders.Add(new ViewComponentFeatureProvider());
             });
}
应用程序部分是对应用程序资源的抽象,可以从中发现MVC特性,如控制器、视图组件或标记帮助器

// using System.Reflection;
// using Microsoft.AspNetCore.Mvc.ApplicationParts;
// using Microsoft.AspNetCore.Mvc.ViewComponents;

public void ConfigureServices(IServiceCollection services)
{
     ...
     var assembly = typeof(ClassInYourLibrary).GetTypeInfo().Assembly;
     var part = new AssemblyPart(assembly);

     services.AddMvc()
             .ConfigureApplicationPartManager(p => {
                p.ApplicationParts.Add(part);
                p.FeatureProviders.Add(new ViewComponentFeatureProvider());
             });
}
将以下内容添加到.csproj中(对于具有视图的库),以将视图作为嵌入资源包含到dll中:

<ItemGroup>
   <EmbeddedResource Include="Views\**\*.cshtml" /> 
</ItemGroup>
// using System.Reflection;
// using Microsoft.AspNetCore.Mvc.ApplicationParts;
// using Microsoft.AspNetCore.Mvc.ViewComponents;

public void ConfigureServices(IServiceCollection services)
{
     ...
     var assembly = typeof(ClassInYourLibrary).GetTypeInfo().Assembly;
     var part = new AssemblyPart(assembly);

     services.AddMvc()
             .ConfigureApplicationPartManager(p => {
                p.ApplicationParts.Add(part);
                p.FeatureProviders.Add(new ViewComponentFeatureProvider());
             });
}

另一种方法是使用
EmbeddedFileProvider
。此方法如中所述。

ASP.NET核心中有一个概念:

// using System.Reflection;
// using Microsoft.AspNetCore.Mvc.ApplicationParts;
// using Microsoft.AspNetCore.Mvc.ViewComponents;

public void ConfigureServices(IServiceCollection services)
{
     ...
     var assembly = typeof(ClassInYourLibrary).GetTypeInfo().Assembly;
     var part = new AssemblyPart(assembly);

     services.AddMvc()
             .ConfigureApplicationPartManager(p => {
                p.ApplicationParts.Add(part);
                p.FeatureProviders.Add(new ViewComponentFeatureProvider());
             });
}
应用程序部分是对应用程序资源的抽象,可以从中发现MVC特性,如控制器、视图组件或标记帮助器

// using System.Reflection;
// using Microsoft.AspNetCore.Mvc.ApplicationParts;
// using Microsoft.AspNetCore.Mvc.ViewComponents;

public void ConfigureServices(IServiceCollection services)
{
     ...
     var assembly = typeof(ClassInYourLibrary).GetTypeInfo().Assembly;
     var part = new AssemblyPart(assembly);

     services.AddMvc()
             .ConfigureApplicationPartManager(p => {
                p.ApplicationParts.Add(part);
                p.FeatureProviders.Add(new ViewComponentFeatureProvider());
             });
}
将以下内容添加到.csproj中(对于具有视图的库),以将视图作为嵌入资源包含到dll中:

<ItemGroup>
   <EmbeddedResource Include="Views\**\*.cshtml" /> 
</ItemGroup>
// using System.Reflection;
// using Microsoft.AspNetCore.Mvc.ApplicationParts;
// using Microsoft.AspNetCore.Mvc.ViewComponents;

public void ConfigureServices(IServiceCollection services)
{
     ...
     var assembly = typeof(ClassInYourLibrary).GetTypeInfo().Assembly;
     var part = new AssemblyPart(assembly);

     services.AddMvc()
             .ConfigureApplicationPartManager(p => {
                p.ApplicationParts.Add(part);
                p.FeatureProviders.Add(new ViewComponentFeatureProvider());
             });
}


另一种方法是使用
EmbeddedFileProvider
。此方法在中进行了说明。

如果要使用来自其他程序集的视图,除了要使用ViewComponentFeatureProvider外,还必须使用EmbeddedFileProvider

// using System.Reflection;
// using Microsoft.AspNetCore.Mvc.ApplicationParts;
// using Microsoft.AspNetCore.Mvc.ViewComponents;

public void ConfigureServices(IServiceCollection services)
{
     ...
     var assembly = typeof(ClassInYourLibrary).GetTypeInfo().Assembly;
     var part = new AssemblyPart(assembly);

     services.AddMvc()
             .ConfigureApplicationPartManager(p => {
                p.ApplicationParts.Add(part);
                p.FeatureProviders.Add(new ViewComponentFeatureProvider());
             });
}
因此,完整的ConfigureServices应该类似于:

// using System.Reflection;
// using Microsoft.AspNetCore.Mvc.ApplicationParts;
// using Microsoft.AspNetCore.Mvc.ViewComponents;

public void ConfigureServices(IServiceCollection services)
{
     ...
     var assembly = typeof(ClassInYourLibrary).GetTypeInfo().Assembly;
     var part = new AssemblyPart(assembly);

     services.AddMvc()
             .ConfigureApplicationPartManager(p => {
                p.ApplicationParts.Add(part);
                p.FeatureProviders.Add(new ViewComponentFeatureProvider());
             });
}
        ...
        var assembly = typeof( Iramon.Web.Common.ViewComponents.ActiveAccountViewComponent).GetTypeInfo().Assembly;
        var part = new AssemblyPart(assembly);

        services.AddMvc()
            .ConfigureApplicationPartManager(p => {                    
                p.ApplicationParts.Add(part);
                p.FeatureProviders.Add(new ViewComponentFeatureProvider());                    
            });        

        var embeddedFileProvider = new EmbeddedFileProvider(
            assembly
        );

        //Add the file provider to the Razor view engine
        services.Configure<RazorViewEngineOptions>(options =>
        {                
            options.FileProviders.Add(embeddedFileProvider);
        });    
。。。
var assembly=typeof(Iramon.Web.Common.ViewComponents.ActiveAccountViewComponent.GetTypeInfo().assembly;
var part=新的组装件(组装件);
services.AddMvc()
.ConfigureApplicationPartManager(p=>{
p、 应用程序部件。添加(部件);
p、 添加(新的ViewComponentFeatureProvider());
});        
var embeddedFileProvider=新的embeddedFileProvider(
装配
);
//将文件提供程序添加到Razor视图引擎
配置(选项=>
{                
options.FileProviders.Add(embeddedFileProvider);
});    

如果要使用来自其他程序集的视图,则必须在ViewComponentFeatureProvider中使用嵌入式文件提供程序

// using System.Reflection;
// using Microsoft.AspNetCore.Mvc.ApplicationParts;
// using Microsoft.AspNetCore.Mvc.ViewComponents;

public void ConfigureServices(IServiceCollection services)
{
     ...
     var assembly = typeof(ClassInYourLibrary).GetTypeInfo().Assembly;
     var part = new AssemblyPart(assembly);

     services.AddMvc()
             .ConfigureApplicationPartManager(p => {
                p.ApplicationParts.Add(part);
                p.FeatureProviders.Add(new ViewComponentFeatureProvider());
             });
}
因此,完整的ConfigureServices应该类似于:

// using System.Reflection;
// using Microsoft.AspNetCore.Mvc.ApplicationParts;
// using Microsoft.AspNetCore.Mvc.ViewComponents;

public void ConfigureServices(IServiceCollection services)
{
     ...
     var assembly = typeof(ClassInYourLibrary).GetTypeInfo().Assembly;
     var part = new AssemblyPart(assembly);

     services.AddMvc()
             .ConfigureApplicationPartManager(p => {
                p.ApplicationParts.Add(part);
                p.FeatureProviders.Add(new ViewComponentFeatureProvider());
             });
}
        ...
        var assembly = typeof( Iramon.Web.Common.ViewComponents.ActiveAccountViewComponent).GetTypeInfo().Assembly;
        var part = new AssemblyPart(assembly);

        services.AddMvc()
            .ConfigureApplicationPartManager(p => {                    
                p.ApplicationParts.Add(part);
                p.FeatureProviders.Add(new ViewComponentFeatureProvider());                    
            });        

        var embeddedFileProvider = new EmbeddedFileProvider(
            assembly
        );

        //Add the file provider to the Razor view engine
        services.Configure<RazorViewEngineOptions>(options =>
        {                
            options.FileProviders.Add(embeddedFileProvider);
        });    
。。。
var assembly=typeof(Iramon.Web.Common.ViewComponents.ActiveAccountViewComponent.GetTypeInfo().assembly;
var part=新的组装件(组装件);
services.AddMvc()
.ConfigureApplicationPartManager(p=>{
p、 应用程序部件。添加(部件);
p、 添加(新的ViewComponentFeatureProvider());
});        
var embeddedFileProvider=新的embeddedFileProvider(
装配
);
//将文件提供程序添加到Razor视图引擎
配置(选项=>
{                
options.FileProviders.Add(embeddedFileProvider);
});    

我成功地使用以下框架将视图编译成dll:

// using System.Reflection;
// using Microsoft.AspNetCore.Mvc.ApplicationParts;
// using Microsoft.AspNetCore.Mvc.ViewComponents;

public void ConfigureServices(IServiceCollection services)
{
     ...
     var assembly = typeof(ClassInYourLibrary).GetTypeInfo().Assembly;
     var part = new AssemblyPart(assembly);

     services.AddMvc()
             .ConfigureApplicationPartManager(p => {
                p.ApplicationParts.Add(part);
                p.FeatureProviders.Add(new ViewComponentFeatureProvider());
             });
}

// using System.Reflection;
// using Microsoft.AspNetCore.Mvc.ApplicationParts;
// using Microsoft.AspNetCore.Mvc.ViewComponents;

public void ConfigureServices(IServiceCollection services)
{
     ...
     var assembly = typeof(ClassInYourLibrary).GetTypeInfo().Assembly;
     var part = new AssemblyPart(assembly);

     services.AddMvc()
             .ConfigureApplicationPartManager(p => {
                p.ApplicationParts.Add(part);
                p.FeatureProviders.Add(new ViewComponentFeatureProvider());
             });
}

整个框架可能对您没有用处,但您肯定可以分析源代码,看看该框架是如何实现的。

我已经成功地使用以下框架将视图编译到dll中:

// using System.Reflection;
// using Microsoft.AspNetCore.Mvc.ApplicationParts;
// using Microsoft.AspNetCore.Mvc.ViewComponents;

public void ConfigureServices(IServiceCollection services)
{
     ...
     var assembly = typeof(ClassInYourLibrary).GetTypeInfo().Assembly;
     var part = new AssemblyPart(assembly);

     services.AddMvc()
             .ConfigureApplicationPartManager(p => {
                p.ApplicationParts.Add(part);
                p.FeatureProviders.Add(new ViewComponentFeatureProvider());
             });
}

// using System.Reflection;
// using Microsoft.AspNetCore.Mvc.ApplicationParts;
// using Microsoft.AspNetCore.Mvc.ViewComponents;

public void ConfigureServices(IServiceCollection services)
{
     ...
     var assembly = typeof(ClassInYourLibrary).GetTypeInfo().Assembly;
     var part = new AssemblyPart(assembly);

     services.AddMvc()
             .ConfigureApplicationPartManager(p => {
                p.ApplicationParts.Add(part);
                p.FeatureProviders.Add(new ViewComponentFeatureProvider());
             });
}

整个框架可能对您没有用处,但您肯定可以分析源代码,看看该框架是如何实现的。

亲爱的@Set,我遇到了这个错误,两个星期以来我都无法解决这个问题:找不到包“整体主义.框架”的编译库位置。我如何查看DLL的嵌入式资源?我的意思是,我正在使用
ildasm
查看DLL包的内部,但我看到的只是代码,而我看不到资源。根据我将
Microsoft.AspNetCore.all
更改为
2.0.3
版本,至少我不再有那个错误了。现在我有一个错误,
cshtml布局找不到
。亲爱的@Set,我收到了这个错误,两个星期以来我都无法解决这个问题:找不到包'Holism.Framework'的编译库位置。我如何查看DLL的嵌入资源?我的意思是,我正在使用
ildasm
查看DLL包的内部,但我看到的只是代码,而我看不到资源。根据我将
Microsoft.AspNetCore.all
更改为
2.0.3
版本,至少我不再有那个错误了。现在我有一个错误,
cshtml布局找不到
// using System.Reflection;
// using Microsoft.AspNetCore.Mvc.ApplicationParts;
// using Microsoft.AspNetCore.Mvc.ViewComponents;

public void ConfigureServices(IServiceCollection services)
{
     ...
     var assembly = typeof(ClassInYourLibrary).GetTypeInfo().Assembly;
     var part = new AssemblyPart(assembly);

     services.AddMvc()
             .ConfigureApplicationPartManager(p => {
                p.ApplicationParts.Add(part);
                p.FeatureProviders.Add(new ViewComponentFeatureProvider());
             });
}