2

属性をルーティングに使用しています。それは関連している、私は知らない。_Rayout.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.

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

編集をGlobal.asaxの_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(より正確に言えば、@RenderBody()方法を含む任意の.cshtmlファイル)によって処理されますマスターページとしてのMVCフレームワーク(別名レイアウトビュー) - 他のページをレンダリングするためのテンプレートとして使用されるページです。したがって、直接要求することはできません。

レイアウトビューを参照する適切な方法は、それを使用する任意のビューからレイアウトプロパティを設定することです。たとえば、Index.cshtmlという名前のビューがあるとします。その中には、次の行を配置します:~/Views/_ViewStart.cshtml

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

あなたがレイアウトビューは、プロジェクト内のすべてのビューに適用したいしかし、その後、youwillがファイルに上記のコードを追加する必要がある場合

上記をすべて完了したら、になりますので、ビューがレイアウトページを指していないようにコントローラを変更してください。これは、アクションメソッドの名前が_Layoutであることを確認するか、アクション内でView()メソッドへの呼び出しで関心のあるビューの名前を渡すことによって実行できます。

+0

Index.cshtmlには、 '_Layout ="〜/ Views/Shared/_Layout.cshtml "; 'がありません。今私は追加しました。 ** _ ViewStart.cstml **にはこのコードスニペットがあります。 ディレクトリパスが正しいと確信しています。しかし、私は同じエラーがあります:/ –

+0

Index.cshtmlファイルでは、 'Layout = ...' NOT '_Layout = ...'になります。 '_ 'は削除する必要があります。 –

+0

はい、私は削除しましたが、私は同じです。また私は私の質問を編集しました。もう一度確認できますか? –

関連する問題