gpt4 book ai didi

c# - 未找到 View 'Index' 或其母版

转载 作者:太空狗 更新时间:2023-10-29 18:00:28 37 4
gpt4 key购买 nike

我是 C# MVC 项目类型的新手,当我创建一个空的 C# MVC 项目时,我注意到以下错误:

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

我在 Views 文件夹下有“Index.cshtml”文件。为什么MVC引擎不直接在Views文件夹下找?我该如何解决这个问题?

我的 RouteConfig.cs 内容是:

 routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = <ControllerName>, action = "Index", id = UrlParameter.Optional }
);

我的 Controller 内容:

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

最佳答案

MVC 在 vi​​ews 文件夹下寻找 View (如 Index),但它们也必须在以其 Controller 命名的文件夹下(除了一些异常(exception),如 partials)。

这就是您要在项目中遵循的结构

Controllers (folder)
HomeController (.cs)
AccountController (.cs)

Views (folder)
Home (folder)
Index (.cshtml)
Account (folder)
Index (.cshtml)

关于c# - 未找到 View 'Index' 或其母版,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25209990/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com