本文介绍了无法直接请求_Layout.cshtml,因为它会调用_RenderBody.方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I using attribute to routing. Is that relevant, I don't know.

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

When I don't use "Route" attribute, _Layaout() action in shared controller doesn't work but page is rendering.

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

            return View();
        }
    }

当我使用"Route"属性时,它确实起作用,但出现以下错误:

When I use "Route" attribute it does work but I getting following error:

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

        return View();
    }
}

也global.asax

Also 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

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

Index.cshtml:

@model OgrenciEvi.Models.ViewModel

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

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

_ViewStart.cshtml:

_ViewStart.cshtml:

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

路径图像

推荐答案

_Layout.cshtml(更正确地放置,包含@RenderBody()方法的任何.cshtml文件)视为母版页(也称为布局)视图)-这是用作模板来渲染其他页面的页面.因此无法直接请求.

_Layout.cshtml (more correctly put, any .cshtml file that contains @RenderBody() method) is treated by the MVC framework as a master page (a.k.a layout view) - which is a page used as template to render other pages. Thus it cannot be requested directly.

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

The proper way to refer a layout view is to set the layout property from within any view that will use it. For example: suppose you have a view called Index.cshtml; inside it, you will put the following line:

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

但是,如果您希望将布局视图应用于项目中的所有视图,则需要在文件中添加以上代码片段:~/Views/_ViewStart.cshtml

If however you want the layout view to apply to all views in your project, then youwill need to add the above code snippet in the file: ~/Views/_ViewStart.cshtml

完成上述所有操作后,您进行修改,以使视图不指向布局页面.可以通过确保没有将任何操作方法命名为_Layout来完成此操作,或者在操作内部对View()方法的调用中传入感兴趣的视图的名称.

Once you've done all the above, you should modify your controller so that no view points to a layout page. This can be done by either making sure no action method is named _Layout, or you pass in the name of a view of interest in the call to View() method inside your action.

这篇关于无法直接请求_Layout.cshtml,因为它会调用_RenderBody.方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 18:40