我是第一次尝试 ASP.NET Core RC2。我设法让路由到我的 Controller 正常工作——如果 Controller 返回 Content("hello world")
,我会看到字符串“hello world”返回到我的浏览器。
如果 Controller 返回 View()
,我会得到一个错误
The View 'Index' was not found. The following locations were searched: /Views/Controller/Index.cshtml ...
我确认 View 位于文件夹结构中的正确位置,遵循典型约定。我知道如何在其他版本的 ASP.NET 中实现这一点。
不过,这是我第一次使用 ASP.NET Core,我正在尝试手动配置它,所以我想知道我缺少什么 - 也许我需要一些东西来在管道中注册 Razor,或者注册模板的搜索路径?
在我的 project.json
中,我有 Microsoft.AspNetCore.Mvc
和 Microsoft.AspNetCore.Razor
的依赖项。
最佳答案
ASP.NET Core RC2 的工作 Program.cs
文件如下所示:
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.Build();
host.Run();
}
}
如果 ASP.NET Core 提示无法找到文件,请确保 UseContentRoot(Directory.GetCurrentDirectory())
存在。这将设置 Razor 用于搜索 View 的“基本”路径。
关于.net - 找不到 Razor 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37688127/