2013-09-27 20 views
0

我使用了MVC 4模板示例,並發現了一個奇怪的「精選」部分。這些代碼是從_Index.cshtml:MVC應用示例中的「精選」奇怪部分

@section featured { 
    <section class="featured"> 
     <div class="content-wrapper"> 
      <hgroup class="title"> 
       <h1>@ViewBag.Title.</h1> 
       <h2>@ViewBag.Message</h2> 
      </hgroup> 
      <p> 
       To learn more about ASP.NET MVC visit 
       <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>. 
       The page features <mark>videos, tutorials, and samples</mark> to help you get the most from ASP.NET MVC. 
       If you have any questions about ASP.NET MVC visit 
       <a href="http://forums.asp.net/1146.aspx/1?MVC" title="ASP.NET MVC Forum">our forums</a>. 
      </p> 
     </div> 
    </section> 
} 
<h3>We suggest the following:</h3> 
<ol class="round"> 
    <li class="one"> 
     <h5>Getting Started</h5> 
     ASP.NET MVC gives you a powerful, patterns-based way to build dynamic websites that 
     enables a clean separation of concerns and that gives you full control over markup 
     for enjoyable, agile development. ASP.NET MVC includes many features that enable 
     fast, TDD-friendly development for creating sophisticated applications that use 
     the latest web standards. 
     <a href="http://go.microsoft.com/fwlink/?LinkId=245151">Learn more…</a> 
    </li> 

    <li class="two"> 
     <h5>Add NuGet packages and jump-start your coding</h5> 
     NuGet makes it easy to install and update free libraries and tools. 
     <a href="http://go.microsoft.com/fwlink/?LinkId=245153">Learn more…</a> 
    </li> 

    <li class="three"> 
     <h5>Find Web Hosting</h5> 
     You can easily find a web hosting company that offers the right mix of features 
     and price for your applications. 
     <a href="http://go.microsoft.com/fwlink/?LinkId=245157">Learn more…</a> 
    </li> 
</ol> 

當運行使用Visual Studio開發服務器上本地主機環境中,該網站,部分「特色」呈現,但如果我pulish這個網站上我的IIS,這部分不會被渲染。我不知道爲什麼?

Section'featured'是MVC中的一個隱藏功能嗎? MVC中有沒有隱藏的功能?請幫助我更多地瞭解MVC。謝謝。

+0

請仔細閱讀我的問題。在IIS上部署的此MVC模板示例在Visual Studio Development Server上運行時具有與UI不同的UI。 –

回答

1

確保在共享/ _Layout.cshtml:

@RenderSection("featured ", required: false) 

@RenderSection("featured ", required: true) 
+0

請記住我不會更改代碼,但是在IIS和Visual Studio Development Server上有關於UI的區別? –

2

一個老問題,但我認爲它沒有正確解答。

「精選」只是一個字符串,標識網站的一部分,它不是關鍵字。 Visual Studio附帶的MVC示例是這樣製作的,但絕不是MVC中的祕密或特殊部分。它只是用於選擇性地隱藏或顯示該部分與藍色背景:

http://i1.asp.net/umbraco-beta-media/38980/image001.png

在這個例子中,_Layout.chtml有這樣的代碼:

<div id="body"> 
    @RenderSection("featured", required: false) 
    <section class="content-wrapper main-content clear-fix"> 
     @RenderBody() 
    </section> 
</div> 

這會使以下內容:

<div id="body"> 
    <section class="featured"> 
     /* Whatever you have in your view inside the @section featured { } */ 
    </section> 
    <section class="content-wrapper main-content clear-fix"> 
     /* Whatever you have in your view outside any @section */ 
    </section> 
</div> 

再說一遍,它的內容是「精選」,你可以用你選擇的其他詞來代替它。 ;)

有關章節的更多信息和Render方法here