不允许循环文件引用。 [英] Circular file references are not allowed.

查看:98
本文介绍了不允许循环文件引用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在三个差异目录中创建了3个用户控件(Comment,Comment1,Comment2),如

Comment.ascx

Comment1.ascx

Comment2.ascx





按以下方式参考:



Comment.ascx> Comment1.ascx

Comment1.ascx> Comment2.ascx

Comment2.ascx> Comment.ascx





得到如下错误:

循环文件引用是不允许。 





根据我设置的一些论坛

批在网络配置文件中=false

但我仍然收到错误。

有没有解决办法?

解决方案

循环引用的问题在于编译器无法决定首先编译哪个组件...

所以唯一的选择是通过重新设计应用程序来删除这种循环依赖关系......

1。据我所知,不可能有循环引用。



2.解决方案是重新设计控件以避免此循环引用。


您可以使用递归代码执行此操作。实际的解决方案将根据您的数据以及您如何访问它而改变,但这是要点。所有文件都在网站的根目录中。



CommentObject.cs



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;

命名空间 MyNamespace
{
public class CommentObject
{
public int ID { get ; set ; }
public int ParentID { get ; set ; }
public string 作者{ get ; set ; }
public string 正文{ get ; set ; }
public List< CommentObject>回复{获取; set ; }
}
}





Comment.ascx



 <%@    控制   语言  =  C#    AutoEventWireup   =  true    CodeBehind   =  Comment.ascx.cs   继承  =  MyNamespace.Comment   %>  
< div style = border:1px dashed #aaaaaa; margin-left:30px; >
< h2 > < asp:Literal ID = LiteralAuthor runat = server / > < / h2 >

< div > < asp:Literal ID = LiteralBody runat = server / > < < span class =code-leadattribute> / div >

< ; asp:PlaceHolder ID = PlaceReplies runat = ser ver / >
< / div >





Comment.ascx.cs



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;

命名空间 MyNamespace
{
public partial class 注释:System.Web.UI.UserControl
{
public CommentObject CommentObject { get ; set ; }

受保护 void Page_Load( object sender,EventArgs e)
{
LiteralAuthor.Text = this .CommentObject.Author;
LiteralBody.Text = this .CommentObject.Body;
}
}
}





TestPage.aspx



 <  表格    id   =  form1    runat   =  server >  

< asp:PlaceHolder ID = PlaceComments runat = server / >

< / form >





TestPage.aspx.cs



  protected   void  Page_Load( object  sender,EventArgs e)
{
< span class =code-comment> // 建立评论树
/ / 如何执行此操作取决于您的数据结构,您可能
// 甚至想在ShowComments方法中动态加载数据
List< CommentObject> comments = new List< CommentObject>();

comments.Add( new CommentObject {ID = 1 ,作者= < span class =code-string> 作者A,Body = < span class =code-string> Comment 1});
comments.Add( new CommentObject {ID = 2 ,作者= 作者B,Body = 评论2});
comments.Add( new CommentObject {ID = 3 ,作者= 作者C,Body = 评论3});
comments.Add( new CommentObject {ID = 4 ,作者= 作者D,Body = 评论4});

// 添加对评论2的回复
CommentObject comment =评论。(c = > c.ID == 2 );
comment.Replies = new List< CommentObject>();
comment.Replies.Add( new CommentObject {ID = 5 ,作者= 作者A,Body = 评论5});
comment.Replies.Add( new CommentObject {ID = 6 ,作者= 作者C,Body = 评论6});
comment.Replies.Add( new CommentObject {ID = 7 ,作者= 作者D,Body = 评论7});

// 添加对评论6的回复
comment = comment .Replies。 Single (c = > c.ID == 6 );
comment.Replies = new List< CommentObject>();
comment.Replies.Add( new CommentObject {ID = 8 ,作者= 作者A,Body = 评论8});
comment.Replies.Add( new CommentObject {ID = 9 ,作者= 作者F,Body = 评论9});

ShowComments(PlaceComments,comments);
}

private void ShowComments(PlaceHolder placeHolder,List< CommentObject> comments)
{
foreach (CommentObject comment in comments)
{
评论ctlComment =(Comment)LoadControl( 〜/ Comment.ascx );
ctlComment.CommentObject = comment;

placeHolder.Controls.Add(ctlComment);

if (comment.Replies!= null && comment。 Replies.Any())
{
ShowComments((PlaceHolder)ctlComment.FindControl( PlaceReplies ),comment.Replies);
}
}
}


I have created 3 user controls in three difference directories(Comment,Comment1,Comment2) like
Comment.ascx
Comment1.ascx
Comment2.ascx


Reference those as per give below:

Comment.ascx > Comment1.ascx
Comment1.ascx > Comment2.ascx
Comment2.ascx > Comment.ascx


And getting error like:

Circular file references are not allowed.



As per some of forum I have set

batch="false"

in web config file but still I am getting error.
Is there any solution for that ?

解决方案

The problem with circular references is that the compiler can not decide which component compile first...
So the only option is to remove such circular dependencies by re-design your application...


1. As I know is not possible to have circular references.

2. The solution is to redesign you controls to avoid this circular references.


You can do this using recursive code. The actual solution will change depending on what your data looks like and how you access it, but this is the gist. All files are in the root of the site.

CommentObject.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MyNamespace
{
    public class CommentObject
    {
        public int ID { get; set; }
        public int ParentID { get; set; }
        public string Author { get; set; }
        public string Body { get; set; }
        public List<CommentObject> Replies { get; set; }
    }
}



Comment.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Comment.ascx.cs" Inherits="MyNamespace.Comment" %>
<div style="border: 1px dashed #aaaaaa; margin-left:30px;">
    <h2><asp:Literal ID="LiteralAuthor" runat="server" /></h2>

    <div><asp:Literal ID="LiteralBody" runat="server" /></div>

    <asp:PlaceHolder ID="PlaceReplies" runat="server" />
</div>



Comment.ascx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyNamespace
{
    public partial class Comment : System.Web.UI.UserControl
    {
        public CommentObject CommentObject { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            LiteralAuthor.Text = this.CommentObject.Author;
            LiteralBody.Text = this.CommentObject.Body;
        }
    }
}



TestPage.aspx

<form id="form1" runat="server">

<asp:PlaceHolder ID="PlaceComments" runat="server" />

</form>



TestPage.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    // build up a tree of comments
    // how you do this depends on your data structure, you might
    // even want to load data on the fly in the ShowComments method instead
    List<CommentObject> comments = new List<CommentObject>();

    comments.Add(new CommentObject { ID = 1, Author = "Author A", Body = "Comment 1" });
    comments.Add(new CommentObject { ID = 2, Author = "Author B", Body = "Comment 2" });
    comments.Add(new CommentObject { ID = 3, Author = "Author C", Body = "Comment 3" });
    comments.Add(new CommentObject { ID = 4, Author = "Author D", Body = "Comment 4" });

    // add replies to comment 2
    CommentObject comment = comments.Single(c => c.ID == 2);
    comment.Replies = new List<CommentObject>();
    comment.Replies.Add(new CommentObject { ID = 5, Author = "Author A", Body = "Comment 5" });
    comment.Replies.Add(new CommentObject { ID = 6, Author = "Author C", Body = "Comment 6" });
    comment.Replies.Add(new CommentObject { ID = 7, Author = "Author D", Body = "Comment 7" });

    // add replies to comment 6
    comment = comment.Replies.Single(c => c.ID == 6);
    comment.Replies = new List<CommentObject>();
    comment.Replies.Add(new CommentObject { ID = 8, Author = "Author A", Body = "Comment 8" });
    comment.Replies.Add(new CommentObject { ID = 9, Author = "Author F", Body = "Comment 9" });

    ShowComments(PlaceComments, comments);
}

private void ShowComments(PlaceHolder placeHolder, List<CommentObject> comments)
{
    foreach(CommentObject comment in comments)
    {
        Comment ctlComment = (Comment) LoadControl("~/Comment.ascx");
        ctlComment.CommentObject = comment;

        placeHolder.Controls.Add(ctlComment);

        if (comment.Replies != null && comment.Replies.Any())
        {
            ShowComments((PlaceHolder)ctlComment.FindControl("PlaceReplies"), comment.Replies);
        }
    }
}


这篇关于不允许循环文件引用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆