2

我使用屬性來路由。這是相關的,我不知道。_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"; 
} 

Path Image

回答

0

_Layout.cshtml(更正確地說,任何.cshtml文件,它包含@RenderBody()方法)通過處理MVC框架作爲母版頁(又名佈局視圖) - 這是一個用作模板來渲染其他頁面的頁面。因此不能直接要求。

引用佈局視圖的正確方法是從將使用它的任何視圖中設置佈局屬性。例如:假設你有一個叫做Index.cshtml的視圖;在它裏面,你就會把下面一行:

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

然而,如果你想要的佈局視圖應用到項目中的所有觀點,那麼屆時你需要添加上面的代碼片段文件中:~/Views/_ViewStart.cshtml

完成上述所有操作後,應該修改您的控制器,以便沒有視圖指向佈局頁面。這可以通過確保沒有任何操作方法被命名爲_Layout,或者在您的操作中傳入View()方法中的感興趣視圖的名稱來完成。

+0

Index.cshtml還沒有'_Layout =「〜/ Views/Shared/_Layout.cshtml」;'。現在我添加了。此外** _ ViewStart.cstml **有這段代碼片段。 我確定目錄路徑是正確的。但我得到了同樣的錯誤:/ –

+0

在Index.cshtml文件中,它應該是'Layout = ...'NOT'_Layout = ...'。 '_'應該被刪除。 –

+0

是的,我刪除了,但我得到了同樣的。我還編輯了我的問題。你能再次檢查嗎? –

相關問題