在另一个程序集中找到

在另一个程序集中找到

本文介绍了在另一个程序集中找到剃须刀页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在另一个程序集中找到我的Project Razor页面".为此,我编写了以下代码:

I want to locate My Project Razor Pages in another assembly. for doing this I write following code:

public void ConfigureServices(IServiceCollection services)
{
    var adminAssembly = Assembly.Load(new AssemblyName("App"));
    services.AddMvc().AddApplicationPart(adminAssembly).AddRazorOptions(options =>
    {
        var previous = options.CompilationCallback;
        options.CompilationCallback = context =>
        {
            previous?.Invoke(context);

            context.Compilation = context.Compilation.AddReferences(
                MetadataReference.CreateFromFile(typeof(dodo).Assembly.Location));
        };
    });

    services.Configure<RazorViewEngineOptions>(options =>
    {
        options.FileProviders.Add(new EmbeddedFileProvider(Assembly.Load("App")));
        options.FileProviders.Add(new PhysicalFileProvider(@"C:\Users\soheil\Documents\Visual Studio 2017\Projects\WebApplication5\App"));
    });
}

我的解决方案:

在运行localhost:5000/SameTodo时出现以下错误:

when running localhost:5000/SameTodo Get Following Error:

堆栈:

并将PreserveCompilationContext设置为false,但现在可以解决该问题了吗?

and set PreserveCompilationContext to false but now worked how can I solve this problem?

推荐答案

public void ConfigureServices(IServiceCollection services)
{
    var adminAssembly = Assembly.Load(new AssemblyName("App"));
    services.AddMvc().AddApplicationPart(adminAssembly).AddRazorOptions(options =>
    {
        var previous = options.CompilationCallback;
        options.CompilationCallback = context =>
        {
            previous?.Invoke(context);

            var referenceAssemblies = AppDomain.CurrentDomain.GetAssemblies()
                .Where(x => !x.IsDynamic&& !string.IsNullOrEmpty(x.Location))
                .Select(x => MetadataReference.CreateFromFile(x.Location))
                .ToList();

            //add dynamic
            var dynamicAssembly = typeof(DynamicAttribute).Assembly;
                 referenceAssemblies.Add(MetadataReference.CreateFromFile(dynamicAssembly.Location));

            context.Compilation = context.Compilation.AddReferences(referenceAssemblies);
        };
    });

    services.Configure<RazorViewEngineOptions>(options =>
    {
        options.FileProviders.Add(new EmbeddedFileProvider(Assembly.Load("App")));
        options.FileProviders.Add(new PhysicalFileProvider(@"C:\Users\soheil\Documents\Visual Studio 2017\Projects\WebApplication5\App"));
    });
}

这篇关于在另一个程序集中找到剃须刀页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 02:40