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?
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.
Do you need a @using statement in your _ViewImports.cshtml of WebApplication5? Like @using App.SameTodoModel