简体   繁体   中英

Only Content controls are allowed directly in a content page that contains Content controls in ASP.NET

I have an application which has a master page and child pages. My application is working fine on local host (on my intranet). But as soon as I put it on a server that is on the internet, I get the error shown below after clicking on any menus.

Only Content controls are allowed directly in a content page that contains Content controls.

截图

Double and triple check your opening and closing Content tags throughout your child pages.

Confirm that they

  • are in existence
  • are spelled correctly
  • have an ID
  • have runat="server"
  • have the correct ContentPlaceHolderID

I had exactly the same issue. The problem was I had after the ending content tag:

</asp:Content>

Remove all the spaces, line breaks after the last closing tag.

I was facing a similar issue. Are you surrounding your code with the "content" tag ?

<asp:Content>Add your HTML here</asp:Content> 

And have separate content tags for your sections . A head content for the header declaration and a body content for the body declaration .

Another possibilty is line endings. I copied an older version of code from source control which enforced Unix style line endings. Since it wasn't a checkout, it didn't automatically convert the line endings to the DOS/Windows style. The error message was the "Only Content controls are allowed directly ..." error even though the page was layed out properly. It appears that the lack of Windows style line breaks caused the ASPX parser to fail.

I was able to fix it by pasting the code into a line ending agnostic editor (which caused the line endings to be normalized to the Windows style), re-copying it to the clipboard and pasting it back in Visual Studio, after which the page processed without errors.

In the case presented by Tripati Subudhi in the question, it's entirely possible that something about the deploy process used inadvertently converted line endings to the Unix style, leading to the error.

另一个可能的问题是HTML注释,我有这些围绕内容控件 - 我相信ASP.NET将这些转换为幕后的文字控件 - 因此错误我

another potential cause for this error: tags with the wrong case.

changing <asp:content>... to <asp:Content>... fixed the issue in my case.

the reason for the faulty case was the built-in format document function in visual studio 2012(and 2013) with default settings. the setting for this can be changed in Tools->Options->Text Editor->HTML (Web Forms)->Formatting: set the tag capitalization to 'as entered' and studio will no longer destroy your files.

For me, it didn't like that I had an Assembly and a Page directive commented out:

<%--<%@ Assembly Name="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" %>--%>
<%--<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MyPage.aspx.cs" Inherits="MyClass.MyPage" MasterPageFile="~/_layouts/MyProject/MasterPages/MasterPage.master" %>-->

Even though I had a valid Page directive after that, and was not using System.Core for anything. After just removing them, it loaded fine.

For me it was two content controls that had the same ID - the file had been edited outside of Visual Studio, so the auto-rename of duplicate ID didn't happen. This misleading error was highlighting the first image inside the second content control with the same ID as the first - what a wild goose chase!

Copying the entire page and reposting it over itself solved it, because VS at that point then renamed the duplicate control ID.

In my case I forgot to add assembly reference of AjaxControlToolkit.dll.

When I add the reference the error disappeared.

在SharePoint中,它发生在pageLayout未发布之后。

I had a silly syntax error I kept overlooking. There was an extra < at the start of my MasterType tag I couldn't see for the life of me 🤦‍♂️.

<%@ Page Language="vb" AutoEventWireup="true" MasterPageFile="~/Site1.Master" CodeBehind="Default.aspx.vb" Inherits="SomeApp.Web._Default" %>

<<%@ MasterType VirtualPath="~/Site1.Master" %>

Check your document for non-printing characters

My masterpage contained two UTF-8 BOMs right at the start of the file because I pasted the <%@ Master %> directive from another page. I was able to make it work by backspacing them out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM