简体   繁体   中英

MVC page not loading - The resource cannot be found

I just created an empty MVC project and added a masterpage and 1 view(Index). I also created a Controller(HomeController). I also right clicked on Index.aspx view and set this as startup.

However and error is showing up when running the project - "Server Error in '/' Application. The resource cannot be found. Requested URL: /Views/Index.aspx"

HomeController

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

Index

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

<h2>Index</h2>

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="head" runat="server">
    Test
</asp:Content>

MasterPage

<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>

Global

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);
    }

Try and navigate to /Home/Index

http://localhost:PORT/Home/Index

You've added your Controller HomeController this will match the default MVC routing and look for the Index Action Method within the Home Controller. Also ensure that the Home View is within a folder called Home under the Views section of the application.

As a suggestion you may also want to use the Razor View Engine rather than the ASP View Engine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM