_Layout.cshtml不能直接请求,因为它调用了"RenderBody"方法。

4

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

当我不使用“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
            );
        }

Edit:

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

Index.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";
}

路径图片

1个回答

4

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

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

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

如果您希望布局视图适用于项目中的所有视图,则需要将上述代码片段添加到文件:~/Views/_ViewStart.cshtml。完成以上步骤后,您应该修改控制器以确保没有视图指向布局页面。可以通过确保没有任何操作方法命名为_Layout或者在调用您的操作内部的View()方法时传入感兴趣的视图名称来完成此操作。请注意,要保留HTML标记,请勿包含解释。

Index.cshtml没有_Layout = "~/Views/Shared/_Layout.cshtml";。现在我已经添加了。同时**_ViewStart.cstml**有这段代码片段。我确定目录路径是正确的,但是我仍然得到相同的错误 :/ - Kaan Öztürk
在Index.cshtml文件中,应该是Layout = ...而不是_Layout = ...。下划线_应该被移除。 - Soma Mbadiwe
请查看我的回答中的最后一段。你修改了你的操作方法吗?很可能还没有。 - Soma Mbadiwe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接