我正在尝试使用VS代码创建在OSX上运行的ASP.NET Core MVC测试应用程序.
当我访问默认的主页/索引(或任何其他我尝试的视图)时,我得到一个“查看未找到”异常.
当我访问默认的主页/索引(或任何其他我尝试的视图)时,我得到一个“查看未找到”异常.
这是启动配置
public void Configure(IApplicationBuilder app) { // use for development app.UseDeveloperExceptionPage(); app.UseDefaultFiles(); app.UseStaticFiles(); app.UseMvc( routes => { routes.MapRoute( name: "default",template: "{controller=Home}/{action=Index}" ); }); }
而且我在Views / Home / index.cshtml中定义了视图,并且我在project.json中包含以下包
"dependencies": { "Microsoft.NETCore.App": { "version": "1.0.0-rc2-3002702","type": "platform" },"Microsoft.AspNetCore.Razor.Tools" : "1.0.0-preview1-final","Microsoft.AspNetCore.Diagnostics": "1.0.0-rc2-final","Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final","Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final","Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final","Microsoft.AspNetCore.StaticFiles": "1.0.0-rc2-final","Microsoft.AspNetCore.Routing": "1.0.0-rc2-final" },
最后,这是我得到的例外.
System.InvalidOperationException: The view 'Index' was not found. The following locations were searched: /Views/Home/Index.cshtml /Views/Shared/Index.cshtml at Microsoft.AspNetCore.Mvc.ViewEngines.ViewEngineResult.EnsureSuccessful(IEnumerable`1 originalLocations) at Microsoft.AspNetCore.Mvc.ViewResult.<ExecuteResultAsync>d__26.MoveNext() --- End of stack trace from prevIoUs location where exception was thrown --- ...
任何关于我可能会失踪的建议
解决方法
我发现这个丢失的一块.我最终在VS2015中创建了一个ASP.NET Core项目,然后比较差异.原来我从主页的WebHostBuilder中缺少.UseContentRoot(Directory.GetCurrentDirectory()).
加入后:
public static void Main(string[] args) { new WebHostBuilder() .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) .UseStartup<Startup>() .Build() .Run(); }
然后我收到一个关于missingcompilationContext的异常.一旦在project.json中添加,我的视图显示正确.
"buildOptions": { "preserveCompilationContext": true,"emitEntryPoint": true },