【发布时间】:2015-12-29 13:32:54
【问题描述】:
我刚刚创建了一个空的 MVC 项目并添加了一个母版页和 1 个视图(索引)。我还创建了一个控制器(HomeController)。我还右键单击 Index.aspx 视图并将其设置为启动。
但是,运行项目时出现错误 - “'/' 应用程序中的服务器错误。找不到资源。请求的 URL:/Views/Index.aspx”
家庭控制器
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);
}
【问题讨论】:
-
您的视图看起来像 asp.net 但不是 mvc 页面。您还需要将视图添加到 /Views/Home/Index.aspx
标签: c# asp.net-mvc asp.net-mvc-3 controller global