我使用属性进行路由。这是否相关,我不知道。
当我不使用“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";
}
_Layout = "~/Views/Shared/_Layout.cshtml";
。现在我已经添加了。同时**_ViewStart.cstml**有这段代码片段。我确定目录路径是正确的,但是我仍然得到相同的错误 :/ - Kaan ÖztürkLayout = ...
而不是_Layout = ...
。下划线_
应该被移除。 - Soma Mbadiwe