【问题标题】:Response.Write on Server ControlsResponse.Write 在服务器控件上
【发布时间】:2010-06-01 20:13:25
【问题描述】:

我正在创建一个自定义包含方法(加载)来检查文件是否存在。加载功能包括文件(如果存在)或发送电子邮件通知我链接断开(如果不存在)。我遇到的问题是如果包含文件包含服务器控件,它只是将它们显示为纯文本。如果我要尝试将包含文件添加到包含文件,这是一个问题。

在 default.aspx 我有:

<% ResourceMngr.load("~/Includes/menu.inc"); %>

在资源管理器中:

public static void load(String url) {

    string file = HttpContext.Current.Server.MapPath(url);
    if (System.IO.File.Exists(file))
    {
        HttpContext.Current.Response.Write(System.IO.File.ReadAllText(file));
    }
    else
    {
        String body = "This message was sent to inform you that the following includes file is missing: \r\n";
        body += "Referrer URL: " + HttpContext.Current.Request.Url.AbsoluteUri + "\r\n";
        body += "File Path: " + file;
        MailUtil.SendMail("email@email.com", "Missing Includes File", body);
    }
}

因此,如果“menu.inc”还包含&lt;% ResourceMngr.load("~/Includes/test.inc"); %&gt; 标签,它只会在页面上以纯文本形式将其打印出来,而不是尝试包含 test.inc,而页面上的所有其他 html 会完全显示为预期的。如何允许我的包含文件具有服务器控件?

【问题讨论】:

    标签: c# asp.net controls include


    【解决方案1】:

    我假设您来自经典的 asp 或 php 背景。 asp.net 不能以这种方式与包含一起工作。您可能想查找一些基本的 webforms 或 mvc 教程,因为您想使用框架,而不是反对它:-)

    特别是查看如何使用 UserControls(即带有 .ascx 扩展名的那些)

    【讨论】:

    • 当我尝试使用用户控件时,我遇到了与以前相同的问题。例如 以纯文本形式显示在我的页面顶部。我还尝试在
    • @Aaron,我看不到你的代码,所以我在这里假设。但是感觉你仍然缺少一些关于 asp.net(特别是 webforms)工作方式的基本知识。看看网络上的一些资料(比如这个:asp101.com/lessons/usercontrols.asp),然后一旦你完成了这些,如果你仍然遇到问题,请返回结果 :-) 祝你好运!
    猜你喜欢
    • 2011-05-11
    • 2014-07-05
    • 2014-09-22
    • 2011-05-22
    • 2012-07-12
    • 1970-01-01
    • 1970-01-01
    • 2011-10-09
    • 2011-03-02
    相关资源
    最近更新 更多