_Layout.cshtml cannot be requested directly becaus

2019-07-18 09:48发布

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

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();
        }
    }

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();
    }
}

The file "~/Views/Shared/_Layout.cshtml" cannot be requested directly because it calls the "RenderBody" method.

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
            );
        }

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

Path Image

1条回答
三岁会撩人
2楼-- · 2019-07-18 10:29

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

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

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

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.

查看更多
登录 后发表回答