.net - 找不到 Razor 模板

标签 .net asp.net-core asp.net-core-mvc .net-core .net-core-rc2

我是第一次尝试 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.MvcMicrosoft.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/

相关文章:

c# - 如何使用 HttpTaskAsyncHandler

c# - 在 ASP.NET Core 中注入(inject) IUrlHelper

google-chrome - Chrome 更新后获取 "Error with Permissions-Policy header"

asp.net-core - 如何从 ASP.NET Core Web 应用程序中删除 Bower?

c# - 将 aspnet 身份与自定义表一起使用

c# - FormUrlEncodedContent 的大小限制解决方法

.net DrawString/StringFormat 问题

c# - List<T> 如何使用 ReadOnly 列表复制构造函数?

javascript - 带有动态菜单的布局

asp.net-core - 创建基于路由参数的 ASP.Net Core IAuthorizationRequirement