MVC页面未加载 - 无法找到资源

时间:2015-12-29 13:32:54

标签: c# asp.net-mvc asp.net-mvc-3 controller global

我刚刚创建了一个空的MVC项目并添加了一个母版页和一个视图(索引)。我还创建了一个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);
    }

1 个答案:

答案 0 :(得分:2)

尝试导航至/Home/Index

http://localhost:PORT/Home/Index

您已添加了控制器HomeController,这将与默认的MVC路由匹配,并在归属控制器中查找Index操作方法。还要确保主视图位于应用程序的“视图”部分下名为Home的文件夹中。

作为建议,您可能还想使用Razor View Engine而不是ASP View Engine。

相关问题