1

我使用 MVC 4 模板示例并发现了一个奇怪的“特色”部分。这些代码来自 _Index.cshtml:

@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…&lt;/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…&lt;/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…&lt;/a>
    </li>
</ol>

使用 Visual Studio Development Server 在 localhost 环境中运行此网站时,会呈现“特色”部分,但如果我在 IIS 上推送此网站,则不会呈现此部分。我不知道为什么?

“精选”部分是 MVC 中的隐藏功能?MVC 中是否有任何隐藏的功能?请帮助我了解有关 MVC 的更多信息。谢谢。

4

2 回答 2

3

一个老问题,但我发现它还没有得到正确的回答。

“精选”只是一个标识网站部分的字符串,它不是关键字。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方法的更多信息,请点击此处

于 2015-05-22T23:26:22.450 回答
1

确定在 Shared/_Layout.cshtml 中:

 @RenderSection("featured ", required: false)

或者

@RenderSection("featured ", required: true)
于 2013-09-27T02:38:28.650 回答