Locate Razor Pages in another assembly

2019-06-08 02:28发布

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"));
    });
}

my solution:

devenv_2017-10-10_18-44-26

when running localhost:5000/SameTodo Get Following Error:

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.

stack:

The type or namespace name 'SameTodoModel' could not be found (are you missing a using directive or an assembly reference?) + public global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper Html { get; private set; } The type or namespace name 'SameTodoModel' could not be found (are you missing a using directive or an assembly reference?) + public global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary ViewData => (global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary)PageContext?.ViewData; The type or namespace name 'SameTodoModel' could not be found (are you missing a using directive or an assembly reference?) + public SameTodoModel Model => ViewData.Model; The type or namespace name 'SameTodoModel' could not be found (are you missing a using directive or an assembly reference?) + public global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary ViewData => (global::Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary)PageContext?.ViewData;

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

3条回答
可以哭但决不认输i
2楼-- · 2019-06-08 03:06

Instead of newing AssemblyName, use a reference of a type from that assembly:

var adminAssembly = typeof(SameTodoModel).Assembly;

Also, the error message says: 'PreserveCompilationContext' property is not set to false.

Meaning it should be set to true.

查看更多
萌系小妹纸
3楼-- · 2019-06-08 03:24
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"));
    });
}
查看更多
甜甜的少女心
4楼-- · 2019-06-08 03:24

Do you need a @using statement in your _ViewImports.cshtml of WebApplication5? Like @using App.SameTodoModel

查看更多
登录 后发表回答