Locate Razor Pages in another assembly

泄露秘密 提交于 2019-12-23 03:44:10

问题


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:

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?


回答1:


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



回答2:


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:


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



来源:https://stackoverflow.com/questions/46670806/locate-razor-pages-in-another-assembly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!