如何解决“未找到视图"问题? asp.net核心mvc项目中的异常 [英] How do I solve a "view not found" exception in asp.net core mvc project
问题描述
我正在尝试创建一个使用VS Code在OSX上运行的ASP.NET Core MVC测试应用程序. 访问默认的首页/索引(或我尝试过的任何其他视图)时,出现未找到视图"异常.
I'm trying to create a ASP.NET Core MVC test app running on OSX using VS Code. I'm getting a 'view not found' exception when accessing the default Home/index (or any other views I tried).
这是启动配置
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中包含以下软件包
And I have the view defined in Views/Home/index.cshtml
, and I have the following packages included on 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"
},
最后,这是我得到的例外.
And finally, this is the exception I get.
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 --- ...
关于我可能会缺少的任何建议吗?
Any suggestions of what I might be missing ?
推荐答案
我发现了这条缺失的部分.我最终在VS2015中创建了一个ASP.NET Core项目,然后比较了差异.事实证明我在主WebHostBuilder
中缺少.UseContentRoot(Directory.GetCurrentDirectory())
.
I found this missing piece. I ended up creating a ASP.NET Core project in VS2015 and then compare for differences. It turns out I was missing .UseContentRoot(Directory.GetCurrentDirectory())
from WebHostBuilder
in main.
添加此内容后:
public static void Main(string[] args)
{
new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.Build()
.Run();
}
然后我得到了有关缺少preserveCompilationContext
的异常.一旦添加到project.json中,我的视图就会显示正确.
I then got an exception regarding missing preserveCompilationContext
. Once added in project.json my view shows correct.
"buildOptions": {
"preserveCompilationContext": true,
"emitEntryPoint": true
},
这篇关于如何解决“未找到视图"问题? asp.net核心mvc项目中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!