为什么我在 ASP.NET MVC 中收到“指令‘控制’未知”错误?

qua*_*els 6 asp.net asp.net-mvc asp.net-mvc-2

我正在为 ASP.NET MVC 项目开发编辑器页面。我想对创建页面和编辑页面使用一个控件,这样我就不必重复代码。我在里面设置了一个EditorTemplates文件夹/Views/Shared来保存我的模板。我在其中放置了一个 .ascx 文件,名为ArticlePresentation.ascx.

ArticlePresentation.ascx看起来像这样:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<genesis.Core.Presentation.ArticlePresentation>" %>

Testing the control
Run Code Online (Sandbox Code Playgroud)

我的Edit.aspx观点是这样的:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/genesis.Master" Inherits="System.Web.Mvc.ViewPage<genesis.Core.Presentation.ArticlePresentation>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Edit
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Edit</h2>
    <% using (Html.BeginForm()) {%>
        <%: Html.ValidationSummary(true) %>
        <%: Html.EditorFor(ArticlePresentation => ArticlePresentation)%>
    <% } %>

    <div>
        <%: Html.ActionLink("Back to List", "Index") %>
    </div>

</asp:Content>
Run Code Online (Sandbox Code Playgroud)

我可以构建网站而不会出现任何错误,但是,当我运行网站并浏览到编辑页面时,出现此错误:

System.Web.HttpParseException 未被用户代码处理

 Message=The directive 'control' is unknown.

 Source=System.Web

 ErrorCode=-2147467259

 WebEventCode=0

 FileName=C:<path to folder>asp.net mvc\genesis\genesis\Views\Shared\EditorTemplates\ArticlePresentation.aspx

 Line=2

 VirtualPath=/Views/Shared/EditorTemplates/ArticlePresentation.aspx

 StackTrace:

      at System.Web.UI.TemplateParser.ParseString(String text, VirtualPath virtualPath, Encoding fileEncoding)

      at System.Web.UI.TemplateParser.ParseFile(String physicalPath, VirtualPath virtualPath)

      at System.Web.UI.TemplateParser.ParseInternal()

      at System.Web.UI.TemplateParser.Parse()

      at System.Web.Compilation.BaseTemplateBuildProvider.get_CodeCompilerType()

      at System.Web.Compilation.BuildProvider.GetCompilerTypeFromBuildProvider(BuildProvider buildProvider)

      at System.Web.Compilation.BuildProvidersCompiler.ProcessBuildProviders()

      at System.Web.Compilation.BuildProvidersCompiler.PerformBuild()
Run Code Online (Sandbox Code Playgroud)

等等...等等...等等...

我在 google 上搜索寻找解决方案,但到目前为止我发现的所有内容都与在 aspx 页面而不是 ascx 页面中拥有控件有关。

有谁知道可能导致此错误的原因是什么?

Dar*_*rov 6

您收到此错误的原因是您的文件具有.aspx扩展名而不是.ascx. 仔细阅读错误信息:

错误代码=-2147467259

网络事件代码=0

文件名=C:\Documents and Settings\bquakkelaar\Desktop\dropstuff\asp.net mvc\genesis\genesis\Views\Shared\EditorTemplates\ArticlePresentation。ASPX

现在将文件重命名为ArticlePresentation.ascx,一切都会按预期工作。

您也可以替换这一行:

<%: Html.EditorFor(ArticlePresentation => ArticlePresentation)%>
Run Code Online (Sandbox Code Playgroud)

有了这个:

<%: Html.EditorForModel() %>
Run Code Online (Sandbox Code Playgroud)