简体   繁体   中英

MVC Page not showing up, 404 not found

I have a very simple MVC site that is returning a 404 not found error when trying to load a page at the very beginning. I'm looking for some direction to troubleshoot this problem since there is really nothing to go on from the error message.

UPDATE: The problem appears to have been cause by me setting the start page by right-clicking on the file and saying set as start page. This caused Visual Studio to try to load that page directly. When modifying the url to access the page using the routing rules the page will load correctly as suggested by Keltex below.

The error I'm getting is:

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /Views/Other/Index.aspx

Below I have included the code for the various pieces, routing rules are default:

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional}  // Parameter defaults
);

The site is using nested MasterPages, not sure if this is involved with the problem but trying to include as much detail as possible.

I have:

Controllers

  • OtherController

Views:

  • Shared Folder:
    • Site.Master
  • Other Folder:
    • Other.Master
    • Index.aspx

Site.Master Code:

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>
            <asp:ContentPlaceHolder ID="TitleContent" runat="server" />
        </title>
    </head>
    <body>
        <div>
            <asp:ContentPlaceHolder ID="MainContent" runat="server">
            </asp:ContentPlaceHolder>
        </div>
    </body>
</html>

Other.Master Code:

<%@ Master Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewMasterPage" %>

<asp:Content ID="OtherTitle" ContentPlaceHolderID="TitleContent" runat="server">
    OTHER PAGE - MASTER TITLE
    <asp:ContentPlaceHolder ID="OtherPageTitle" runat="server">
    </asp:ContentPlaceHolder>
</asp:Content>

<asp:Content ID="OtherContent" ContentPlaceHolderID="MainContent" runat="server">       
    Some other content.
    <asp:ContentPlaceHolder ID="PageContent" runat="server">    
    </asp:ContentPlaceHolder>
</asp:Content>

Index.aspx Code:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Other/Other.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="IndexTitle" ContentPlaceHolderID="OtherTitle" runat="server">
    Home
</asp:Content>

<asp:Content ID="IndexContent" ContentPlaceHolderID="OtherContent" runat="server">
    Index content
</asp:Content>

OtherController Code

namespace MVCProject.Controllers
{
    public class OtherController : Controller
    {
        //
        // GET: /Member/

        public ActionResult Index()
        {
            // Have also tried:
            // return View("Index", "Other.Master");

            return View();
        }
    }
}

Is there any special URL or route specified in the Project Properties dialog?

This needs to be a route or URL , and not an .aspx page.

替代文字

I think the URL should be:

/Other

not

/Views/Other/Index.aspx

The URLs are not typically prefixed with /Views . This is just the folder in which views are located. Also typically the View Index is not specified since this is the default action. Finally the extension .aspx is usually not specified in MVC. If you want this page to come up as the site's default page, you need to change your routing rules to look like this:

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Other", action = "Index", id = UrlParameter.Optional}  // Parameter defaults
);

(notice the change of the default controller from Home to Other )

The 404 message seems to suggest you are trying to access the view directly in the web browser. Is this correct? Shouldn't you be accessing the url /Other/Index?

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