0

I had previously asked a question about a 404 error being thrown but thanks to help from folks attempting to help me debug, I now have a more specific question to ask.

I see the following error when I load any view that invokes @Html.Partial:

This localhost page can’t be found

In visual studio, I see return View() being hit.

Sample method from HomeController:

    [Route("")]
    public ActionResult Welcome()
    {
        return View();
    }

I'm using Microsoft.AspNet.MVC version 5.2.7 in case it's relevant.

Image of browser error:

enter image description here

RouteConfig:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.MapMvcAttributeRoutes();
    }
}

My directory:

enter image description here

Welcome.cshtml:

@using Newtonsoft.Json
@{
Layout = null;
}    
<!doctype html>
<html class="no-js" lang="">
<head>
    <title>Test</title>
    @Html.Partial("_HeadPartial")
</head>
<body>
  <p></p>
</body>
</html>

_HeadPartial:

    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

Any kind of partial reference:

 @Html.Partial("_AnalyticsPartial")

Gives me the above 404 error; removing the reference allows rendering to succeed. My partials can be empty and still result in a 404.

Why is @Html.Partial causing this?

1 Answer 1

1

The 404 is because it can't find your partial view. You just need to include the full path to the partial view, since it's in a different folder than the current view:

@Html.Partial("~/Views/Shared/_HeadPartial")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.