asp.net-mvc 无法直接请求文件“~/Views/Position/Edit.cshtml”,因为它调用了“RenderSection”方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7808377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:31:31  来源:igfitidea点击:

The file "~/Views/Position/Edit.cshtml" cannot be requested directly because it calls the "RenderSection" method

asp.net-mvcasp.net-mvc-3asp.net-mvc-2razor

提问by Luis Valencia

I am trying to separate all the things that I could reuse in sections, so it would be easier for me to maintain.

我试图将我可以在各个部分中重用的所有东西分开,这样对我来说维护起来会更容易。

However I got this exception: The file "~/Views/Position/Edit.cshtml" cannot be requested directly because it calls the "RenderSection" method

但是我遇到了这个异常:无法直接请求文件“~/Views/Position/Edit.cshtml”,因为它调用了“RenderSection”方法

I created a file called sections.cshtml with the following content:

我创建了一个名为sections.cshtml 的文件,内容如下:

@section scripts{
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
}

And in the _layout.cshtml file I changed it to:

在 _layout.cshtml 文件中,我将其更改为:

<head>
    <meta charset="utf-8" />
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    @RenderSection("scripts", required:false)
    @*<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>*@
</head>

When I go to the view in the browser and check the source code it shows only:

当我转到浏览器中的视图并检查源代码时,它只显示:

<head>
    <meta charset="utf-8" />
    <title>Edit</title>
    <link href="/Content/Site.css" rel="stylesheet" type="text/css" />
</head>

采纳答案by Martin Booth

RenderSection can only exist in Layout files (i.e. master pages)... its purpose is to allow the pages you can request directly to target various sections of a Layout (layout being a file common to all pages which choose to use it) and supply content for these different sections.

RenderSection 只能存在于布局文件(即母版页)中...它的目的是允许您可以直接请求的页面以布局的各个部分为目标(布局是所有选择使用它的页面通用的文件)并提供这些不同部分的内容。

If you want to separate this section out as something which is resuable on many pages you should put it in a partial and replace the rendersection call to something like

如果您想将此部分分离为可在许多页面上重用的内容,您应该将其放在部分中并将 rendersection 调用替换为类似的内容

@Html.Partial("Scripts")

回答by Gerwald

Alternatively you could use helperto separate code you use more often. Especially if you cannot use sectionsbecause of the constraint Martin-Booth mentioned.

或者,您可以使用helper分隔您更经常使用的代码。特别是如果您sections因为 Martin-Booth 提到的约束而无法使用。

@helper Scripts(){
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
}

and the usage is just:

用法只是:

<somehtml />
@Scripts()
<somehtml />