Search code examples
asp.net-mvcvisual-studioiisentity-framework-4

MVC getting a 404 error for page but page still displays correctly


I'm having a very very strange problem. On my localhost MVC site, the page loads and displays fine, but in Firebug the very first line in the Console tab says...

"NetworkError: 404 Not Found - http://localhost:11634/clay-test-3"

... where clay-test-3 is the URL of the page.

I've used routedebugger to check the route, and everything there is working fine. Well, obviously it would have to be, because it's finding its way to the correct controller and the correct action and is generating the view.

Unfortunately on the production site, all that comes up for the same page is just the standard IIS 404 error page, no routedebugger, no errors being logged in Elmah.

If someone could explain to me how (on localhost) I can get a 404 error AND THE PAGE STILL DISPLAYS FINE, that might help me track down the problem.

I'm assuming the issue is within the controller, but I am as yet to find a significant difference between this controller and the other, functioning, controllers on the site.

My local machine is running IIS 10.0, the production box IIS 8.0.

Thanks for your time!


Solution

  • Ok, it looks like these were two separate problems, one which was causing the 404 error for a successfully displayed page in localhost, and another which was causing the 404 error (and the page to not display at all) on my production server.

    The first problem was caused by the following...

    Because the CSS stylesheet I'd been given had a body definition which needed a different class set for different types of pages, ie:

    <body class="home_page">
    

    ... I decided to move the <body> & </body> tags out of the layout template, and into the view, so that the html block in the _LayoutSite.cshtml file looked like this:

    <html>
        ... [various other code]
        @RenderBody()
        @RenderSection("scripts", required: true)
    </html>
    

    Apparently this is a no-no. The body tags NEED to be in the layout template, around @RenderBody().

    <html>
        ... [various other code]
        <body>
            @RenderBody()
            @RenderSection("scripts", required: true)
        </body>
    </html>
    

    So I fixed that and created a container div with the required class in the view. Ie: <div class="home_page">

    The other problem (which is causing the 404 error on the production server) was the result of a bit of code in the view that was copied from another project, which had slightly different routing.

    <a href="@Url.Action("ViewPage", "Page")" id="logo"><img ... etc ... ></a>
    

    I had never tested the link on my localhost Debug version, and in Release there must be a mechanism that stops the page from rendering if not all action/controller links resolve properly.

    Phew! That took a bit of hunting down. Thanks for your answers and comments.