2014-10-01 47 views
2

我想知道为什么对于我的某个视图,我得到错误”Section not defined:“featured2”“。布局的路径是正确的,我定义了featured2在查看部分。cshtml view“Section not undefined:”featured2“

查看

@using System.Data; 
@using System.Data.SqlClient; 
@{ 
ViewBag.Title = "Attempted Unauthorized Access"; 
Layout = "../Shared/Layout2.cshtml"; 
string whoareyoupeople = User.Identity.Name.ToString(); 
DateTime date = DateTime.Now; 
string myerrorstring = "User " + whoareyoupeople + " attempted unauthorized access on " + date + "."; 

    string fileName = "C:\\BillingExport\\SECURITY\\seclog.txt"; 
    using (FileStream fs = new FileStream(fileName, FileMode.Append, FileAccess.Write)){ 
    using (StreamWriter sw = new StreamWriter(fs)) 
    { 
     sw.WriteLine(myerrorstring); 
    } 
} 
} 
@section featured2 { 
<body> 
    <head></head> 
<center><h2 style="color:red">Access Denied for user @User.Identity.Name. You are not authorized to view this application.</h2></center> 
    </body> 
} 

布局2

@{ 
ViewBag.Title = "Layout2"; 
} 

<link runat="server" rel="icon" href="../Content/images/eifavicon.ico" type="image/x-icon" /> 
@RenderSection("featured2") 
@Scripts.Render("~/bundles/jquery") 
@RenderSection("scripts", required: false) 
       <script> 
             browsername = navigator.appName; 
             if (browsername.indexOf("Microsoft") != -1) { 
              browsername = "MSIE"; 
              document.write("<p id='indent2'>You're viewing this page using Internet Explorer. Please use a supported browser (<a href='https://www.google.com/chrome/browser/'>Chrome</a>, <a href='https://www.mozilla.org/en-US/firefox/new/'>Firefox</a>)</p>"); 
             } 

</script> 
+0

您能够成功地建立自己的解决方案?尝试关闭并重新打开视觉工作室,这不应该发生。 – 2014-10-01 14:51:37

+0

是的,它建立。我试过重新启动Visual Studio 2012,没有解决问题。当我在调试器运行时尝试访问视图时,它恰好对着我的Layout2.cshtml,并将featured2突出显示为未定义。 – Dave 2014-10-01 15:10:40

+0

您的视图如何呈现?它在RenderPage()声明中吗? – 2014-10-01 15:27:46

回答

0

ç鲍尔的问题是一个有效的,你从RenderPage渲染这种观点()?

您无法在模板中定义由 RenderPage()渲染的部分。部分实际上只能从Controller传递的实际View 模板中定义。

Similar Stack overflow question/answer

相关问题