我使用屬性來路由。這是相關的,我不知道。_Layout.cshtml不能直接請求,因爲它調用「RenderBody」方法
當我不使用「路由」屬性時,共享控制器中的_Layaout()操作不起作用,但頁面正在呈現。
public class SharedController : Controller
{
// GET: Shared
[AllowAnonymous]
public ActionResult _Layout()
{
return View();
}
}
當我使用 「路線」 屬性它不工作,但我得到以下錯誤:
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.
而且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>
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";
}
Index.cshtml還沒有'_Layout =「〜/ Views/Shared/_Layout.cshtml」;'。現在我添加了。此外** _ ViewStart.cstml **有這段代碼片段。 我確定目錄路徑是正確的。但我得到了同樣的錯誤:/ –
在Index.cshtml文件中,它應該是'Layout = ...'NOT'_Layout = ...'。 '_'應該被刪除。 –
是的,我刪除了,但我得到了同樣的。我還編輯了我的問題。你能再次檢查嗎? –