在另一个程序集中找到剃须刀页面 [英] Locate Razor Pages in another assembly
问题描述
我想在另一个程序集中找到我的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:
缺少一个或多个编译参考.确保您的项目引用的是"Microsoft.NET.Sdk.Web",并且"PreserveCompilationContext"属性未设置为false.
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.
堆栈:
找不到类型或名称空间名称"SameTodoModel"(您是 缺少using指令或程序集引用?) + 全局公共:: Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper HTML {get;私人套装;类型或名称空间名称"SameTodoModel" 找不到(您是否缺少using指令或程序集 参考?) + 全局公共:: Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary ViewData => (全局:: Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary)PageContext?.ViewData; 找不到类型或名称空间名称"SameTodoModel"(您是 缺少using指令或程序集引用?) + 公共SameTodoModel模型=> ViewData.Model;找不到类型或名称空间名称"SameTodoModel"(您是否缺少 使用指令还是程序集引用?) + 全局公共:: Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary ViewData => (全局:: 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 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;
并将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"));
});
}
这篇关于在另一个程序集中找到剃须刀页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!