我正在使用基于.NET 4.7的asp.net核心mvc应用程序。 当我开始生成示例应用程序时,一切正常。
但是我有一个更大的带有插件支持的应用程序,我想使用asp.net mvc作为我的插件之一。
嵌入插件,并从dll文件加载Assembly,然后调用Start()方法启动插件。
当我在此方法中添加asp.net起始代码时,Web应用程序会在出现任何错误的任何请求下启动:
Connection id "0HLJE77KODDC8", Request id "0HLJE77KODDC8:00000001": An unhandled exception was thrown by the application.
System.InvalidOperationException: The view 'Index' was not found. The following locations were searched:
/Views/Home/Index.cshtml
/Views/Shared/Index.cshtml
/Pages/Shared/Index.cshtml
bei Microsoft.AspNetCore.Mvc.ViewEngines.ViewEngineResult.EnsureSuccessful(IEnumerable`1 originalLocations)
bei Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.<ExecuteAsync>d__6.MoveNext()
但是我知道Index.cshtml存在。
我通过以下方式启动asp.net应用
CreateWebHostBuilder(new string[] { }).UseContentRoot(myWebBinPath).Build().Run();
来自通过反射调用的Method。 所有的dll参考汇编都可以很好地解决,但是当我从其他应用程序上下文(例如Environemt CurrentDirectory ...)调用CreateWebHostBuilder时,是否必须设置其他路径?
我不知道框架在哪里查找此视图,因此我无法调整文件路径...
请帮助我:)
答案 0 :(得分:1)
我明白了
ASP.NET想要一个有效的AppDomain.CurrentDomain.BaseDirectory路径到asp.net bin目录。
所以我必须为我的应用程序的特定部分创建一个新的AppDomain。
在我的测试方案中,当我强制更新AppBase路径时,它可以工作:
AppDomain.CurrentDomain.SetData("APPBASE", @"D:\...\HomeDataManagement.Plugin.Service.Web\bin\Debug\net461\");
然后,应用程序环境与独立运行exe文件相同,因此我可以在没有contentRoot路径的情况下调用WebBuilder:
CreateWebHostBuilder(new string[] { }).Build().Run();