我想在另一个程序集中找到My Project Razor Pages。 为此,我写下面的代码:
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
时获取以下错误:
缺少一个或多个编译引用。确保您的项目引用了Microsoft.NET.Sdk.Web&#39;和&#39; PreserveCompilationContext&#39; property未设置为false。
堆栈:
类型或命名空间名称&#39; SameTodoModel&#39;找不到(是你吗? 缺少using指令或程序集引用?) + public global :: Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper Html {get;私人集;类型或命名空间名称&#39; SameTodoModel&#39; 无法找到(您是否错过了使用指令或程序集 参考?) + public global :: Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary ViewData =&gt; (全球:: Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary)的PageContext .ViewData?; 类型或命名空间名称&#39; SameTodoModel&#39;找不到(是你吗? 缺少using指令或程序集引用?) + public SameTodoModel Model =&gt; ViewData.Model;类型或命名空间名称&#39; SameTodoModel&#39;无法找到(你错过了吗? 使用指令或程序集引用?) + public global :: Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary ViewData =&gt; (全球:: Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary)的PageContext .ViewData;
并将PreserveCompilationContext
设置为false但现在有效如何解决此问题?
答案 0 :(得分:4)
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"));
});
}
答案 1 :(得分:0)
不使用新的AssemblyName,而是使用该程序集中类型的引用:
var adminAssembly = typeof(SameTodoModel).Assembly;
此外,错误消息显示:&#39; PreserveCompilationContext&#39; property未设置为false。
意思是它应该设置为true。
答案 2 :(得分:-1)
您是否需要在WebApplication5的_ViewImports.cshtml中使用@using语句?喜欢@using App.SameTodoModel