2

我想知道需要将哪些文件添加到空白的 .NET c# Razor 项目中才能加载我的第一页。

我创建了一个名为 Index.cshtml 的视图,其中包含:

@{
    ViewBag.Title = "Index"; 
}

<h2>@ViewBag.Title</h2>

我创建了一个名为 HomeController.cs 的控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace My_Website.Models
{
    public class HomeController : Controller
    {
        //
        // GET: /Default1/

        public ActionResult Index()
        {
            return View();
        }

    }
}

当我运行此应用程序时,我收到以下错误:

The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/Index.aspx
~/Views/Home/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Home/Index.cshtml
~/Views/Home/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml 

有没有办法将脚本重定向到直接在 View 文件夹中指向 Index.cshtml 而不必放入主目录以满足默认布局?

4

2 回答 2

4

您的视图必须位于 Views 文件夹中,然后是相应 Controller 的子文件夹Views 下名为 Shared 的文件夹。尝试:

Views -> Shared -> Index.cshtml 不是个好主意

Views -> Home -> Index.cshtml

于 2013-01-23T13:59:53.577 回答
1

您可以在操作方法中指定视图文件路径:

return View("~/Views/Index.cshtml");
于 2013-01-23T14:02:49.650 回答