【问题标题】:_Layout.cshtml cannot be requested directly because it calls the "RenderBody" method_Layout.cshtml 不能直接请求,因为它调用了“RenderBody”方法
【发布时间】:2017-11-04 12:14:13
【问题描述】:

我使用属性来路由。这是否相关,我不知道。

当我不使用“Route”属性时,共享控制器中的 _Layaout() 操作不起作用,但页面正在呈现。

public class SharedController : Controller
    {
        // GET: Shared
        [AllowAnonymous]
        public ActionResult _Layout()
        {

            return View();
        }
    }

当我使用“Route”属性时,它确实有效,但出现以下错误:

public class SharedController : Controller
{
    // GET: Shared
    [AllowAnonymous]
    [Route]
    public ActionResult _Layout()
    {

        return View();
    }
}

无法直接请求文件“~/Views/Shared/_Layout.cshtml” 因为它调用了“RenderBody”方法。

还有 global.asax

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

编辑:

_Layout.cshtml

 @model OgrenciEvi.Models.ViewModel

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>@ViewBag.Title - Ogrencievi.net</title>
        <link href="~/Content/Site.css" rel="stylesheet" type="text/css" />
        <link href="~/Content/bootstrap.min.css" rel="stylesheet" type="text/css" />
        <link href="~/Content/font-awesome.min.css" rel="stylesheet" />
        <link href="~/Content/tether.css" rel="stylesheet" type="text/css" />

        <link rel="icon" type="image/png" href="~/Image/favicon.ico" />

        <script src="@Url.Content("~/Scripts/jquery-3.0.0.min.js")"></script>
        <script src="@Url.Content("~/Scripts/tether.js")"></script>
        <script src="@Url.Content("~/Scripts/bootstrap.min.js")"></script>
        <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
        <script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
    </head>
    <body class="p-0">

        @Html.Partial("Navbar")
        <div class="container-fluid p-0">
            @RenderBody()

        </div>
        @Html.Partial("_LoginModal",Model)
        @Html.Partial("_GoogleAnalyticTracker")

    </body>
</html>

索引.cshtml:

@model OgrenciEvi.Models.ViewModel

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
    ViewBag.Title = "Ana Sayfa";
}

@Html.Partial("LandingSection/SearchSection", Model)

_ViewStart.cshtml:

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

Path Image

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-4 model-view-controller asp.net-mvc-5


    【解决方案1】:

    _Layout.cshtml(更准确地说,任何包含@RenderBody() 方法的.cshtml 文件)都被MVC 框架视为母版页(又名布局视图)- 用作呈现其他页面的模板的页面.因此不能直接请求。

    引用布局视图的正确方法是在将使用它的任何视图中设置布局属性。例如:假设您有一个名为Index.cshtml 的视图;在其中,您将输入以下行:

    @{
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    

    如果您希望布局视图应用于项目中的所有视图,则需要在文件中添加上述代码 sn-p:~/Views/_ViewStart.cshtml

    完成上述所有操作后,您应该修改您的控制器,以便没有视图指向布局页面。这可以通过确保没有任何操作方法被命名为_Layout 来完成,或者在您的操作中传递对View() 方法的调用中感兴趣的视图的名称。

    【讨论】:

    • Index.cshtml 没有_Layout = "~/Views/Shared/_Layout.cshtml";。现在我已经添加了。 _ViewStart.cstml 也有这个代码 sn-p。我确定目录路径是正确的。但我得到了同样的错误:/
    • 在Index.cshtml文件中,应该是Layout = ...而不是_Layout = ..._ 应该被删除。
    • 检查我回答的最后一段。您是否对您的操作方法进行了修改?很可能不是。
    猜你喜欢
    • 1970-01-01
    • 2011-12-10
    • 2023-03-12
    • 2012-04-03
    • 2016-07-14
    • 1970-01-01
    • 2022-07-26
    • 2021-08-06
    • 2021-12-30
    相关资源
    最近更新 更多