【问题标题】:Html.Partial causing 404 in MVC ViewsHtml.Partial 在 MVC 视图中导致 404
【发布时间】:2019-01-27 22:39:48
【问题描述】:

我之前曾问过一个关于抛出 404 错误的问题,但感谢尝试帮助我调试的人们的帮助,我现在有一个更具体的问题要问。

我在加载任何调用@Html.Partial 的视图时看到以下错误:

This localhost page can’t be found

在 Visual Studio 中,我看到 return View() 被击中。

来自 HomeController 的示例方法:

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

我正在使用 Microsoft.AspNet.MVC 版本 5.2.7 以防万一。

浏览器错误图片:

路由配置:

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

我的目录:

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

任何类型的部分引用:

 @Html.Partial("_AnalyticsPartial")

给我上面的 404 错误;删除引用允许渲染成功。我的部分可以是空的,但仍然会导致 404。

为什么@Html.Partial 会造成这种情况?

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-partialview


    【解决方案1】:

    404 是因为它找不到您的局部视图。您只需要包含部分视图的完整路径,因为它与当前视图位于不同的文件夹中:

    @Html.Partial("~/Views/Shared/_HeadPartial")
    

    【讨论】:

      猜你喜欢
      • 2014-01-06
      • 1970-01-01
      • 2018-03-22
      • 2012-07-30
      • 2019-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多