[英]MVC page not loading - The resource cannot be found
我刚刚创建了一个空的MVC项目,并添加了一个母版页和1个视图(索引)。 我还创建了一个Controller(HomeController)。 我还右键单击Index.aspx视图,并将其设置为启动。
但是,在运行项目时显示错误-“'/'应用程序中的服务器错误。找不到资源。请求的URL:/Views/Index.aspx”
HomeController的
public ActionResult Index()
{
return View();
}
指数
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<h2>Index</h2>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="head" runat="server">
Test
</asp:Content>
母版
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<link href="../../Content/Site.css" rel="stylesheet" />
<title>Test</title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div class="page">
<div id="header">
</div>
<div id="main">
<asp:ContentPlaceHolder ID="MainContent" runat="server" />
<div id="footer">
</div>
</div>
</div>
</form>
</body>
</html>
全球
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
}
尝试并导航到/Home/Index
http://localhost:PORT/Home/Index
您已经添加了Controller HomeController
它将与默认的MVC路由匹配,并在Home Controller中查找Index
Action Method。 还要确保主视图位于应用程序“视图”部分下名为“主文件夹”的文件夹中。
作为建议,您可能还想使用Razor View Engine而不是ASP View Engine。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.