Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# _无法直接请求Layout.cshtml,因为它调用;“渲染器底座”;方法_C#_Asp.net Mvc_Asp.net Mvc 4_Model View Controller_Asp.net Mvc 5 - Fatal编程技术网

C# _无法直接请求Layout.cshtml,因为它调用;“渲染器底座”;方法

C# _无法直接请求Layout.cshtml,因为它调用;“渲染器底座”;方法,c#,asp.net-mvc,asp.net-mvc-4,model-view-controller,asp.net-mvc-5,C#,Asp.net Mvc,Asp.net Mvc 4,Model View Controller,Asp.net Mvc 5,我使用属性来路由。我不知道这有什么关系 当我不使用“Route”属性时,共享控制器中的\u Layaout()操作不起作用,但页面正在呈现 public class SharedController : Controller { // GET: Shared [AllowAnonymous] public ActionResult _Layout() { return View();

我使用属性来路由。我不知道这有什么关系

当我不使用“Route”属性时,共享控制器中的\u 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>
_ViewStart.cshtml:

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

\u Layout.cshtml
(更准确地说,任何包含
@RenderBody()
方法的
.cshtml
文件)都被MVC框架视为母版页(也称为布局视图)——这是一个用作呈现其他页面的模板的页面。因此,不能直接请求

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

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
但是,如果希望布局视图应用于项目中的所有视图,则需要在文件中添加上述代码段:
~/views/\u ViewStart.cshtml


完成上述所有操作后,应修改控制器,使其不会指向布局页面。这可以通过确保没有操作方法被命名为
\u Layout
,或者在操作中调用
view()
方法时传入感兴趣的视图的名称来实现。

Index.cshtml没有
\u Layout=“~/Views/Shared/\u Layout.cshtml”。现在我补充说。另外_ViewStart.cstml还有这个代码段。我确信目录路径是正确的。但是我得到了相同的错误:/Index.cshtml文件中,它应该是
Layout=…
而不是
\u Layout=…
。应该删除
。检查我答案中的最后一段。你对你的行动方法做了修改吗?很可能不是。
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}