在 ASP.NET MVC 中使用 HtmlTextWriter

发布于 2024-09-13 05:59:06 字数 711 浏览 23 评论 0原文

我正在迁移一些旧代码,其中广泛使用 HtmlTextWriter 来呈现 UI 元素。

我正在迁移代码以使用 ASP.NET MVC 1.0。据我所知,我没有使用任何 HtmlTextWriter 特定功能(例如缩进)。

目前,我正在使用包装方法返回由 HtmlTextWriter 生成的字符串,如下所示:

var sw = new StringWriter();
var xhtmlTextWriter = new XhtmlTextWriter(sw);
GenerateHtml(xhtmlTextWriter);
return sw.ToString();

我的问题是:

  1. 我正在尝试从 ASP.NET 获取 HtmlTextWriter 实例MVC View,但显然连 HtmlHelper 也不使用它。我错过了什么吗?

  2. 每次调用 GenerateHtml 都会生成小的 HTML 片段,通常不超过 1000 个字符,但可能会有很多调用。是否值得将 HtmlTextWriter 依赖代码重写为 StringBuilder?或者,创建一个 HtmlTextWriter 实例,该实例将在所有调用中使用(并在迭代结束时刷新)。

I am migrating some old code where HtmlTextWriter is used extensively to render UI elements.

I am migrating the code to use ASP.NET MVC 1.0. As far as I am aware, I am not using any of the HtmlTextWriter specific function (such as indentation).

Currently, I am using a wrapper method to return string generated by the HtmlTextWriter as follow:

var sw = new StringWriter();
var xhtmlTextWriter = new XhtmlTextWriter(sw);
GenerateHtml(xhtmlTextWriter);
return sw.ToString();

My questions are:

  1. I am trying to get HtmlTextWriter instance from ASP.NET MVC View, but apparently even the HtmlHelper does not use this. Do I miss anything?

  2. Each call to GenerateHtml will generated small HTML pieces, generally not bigger than 1000 characters, but there can be a lot of calls. Is it worth rewriting the HtmlTextWriter dependent code into StringBuilder? Or instead, what about creating a HtmlTextWriter instance which will be used on all calls (and flushed at the end of the iterations).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

椵侞 2024-09-20 05:59:06

我认为使用 helper.ViewContext.writer 会起作用,而不是创建 StringBuider 和 StringWriter 。

那么上面的示例代码将是:

var calendar = new DayPilotCalendar();
if( model != null )
   {
   model.CopyTo( calendar );
   }
if( options != null )
   {
   options.CopyTo( calendar );
   }

HtmlTextWriter writer = new HtmlTextWriter( helper.ViewContext.Writer );

writer.AddAttribute( HtmlTextWriterAttribute.Class, "dayPilot" );
writer.RenderBeginTag( HtmlTextWriterTag.Div );
calendar.RenderControl( writer );
writer.RenderEndTag();  // Close DIV

return( null );         // Don't need to return anything.

免责声明:到目前为止,我只尝试使用 helper.ViewContext.Writer 来生成

    。列表。效果很好。还没有尝试过它来渲染控件。

Instead of creating a StringBuider and StringWriter I think using the helper.ViewContext.writer will work.

Then the above sample code would be:

var calendar = new DayPilotCalendar();
if( model != null )
   {
   model.CopyTo( calendar );
   }
if( options != null )
   {
   options.CopyTo( calendar );
   }

HtmlTextWriter writer = new HtmlTextWriter( helper.ViewContext.Writer );

writer.AddAttribute( HtmlTextWriterAttribute.Class, "dayPilot" );
writer.RenderBeginTag( HtmlTextWriterTag.Div );
calendar.RenderControl( writer );
writer.RenderEndTag();  // Close DIV

return( null );         // Don't need to return anything.

Disclaimer: So far I've only tried using the helper.ViewContext.Writer to produce a <UL> list. It worked fine. Haven't tried it to render controls.

李白 2024-09-20 05:59:06

我这里有一个演示应用程序,它展示了如何在一个 MVC 应用程序。

这是该帖子中的代码示例。

    public static string DayPilot(
        this HtmlHelper helper,
        DayPilotData model,
        DayPilotViewOptions options)
    {
        var calendar = new DayPilotCalendar();
        if (model != null)
        {
            model.CopyTo(calendar);
        }
        if (options != null)
        {
            options.CopyTo(calendar);
        }
        var sb = new System.Text.StringBuilder();
        sb.Append("<div class=\"dayPilot\">"); // allows working around td cellpadding bug in css
        using (var sw = new System.IO.StringWriter(sb))
        {
            using (var tw = new HtmlTextWriter(sw))
            {
                calendar.RenderControl(tw);
            }
        }
        sb.Append("</div>");
        return sb.ToString();
    }

关于#2,如果它没有坏的话......

I have a demo app here which shows how to do this in an MVC app.

Here's a code sample, from that post.

    public static string DayPilot(
        this HtmlHelper helper,
        DayPilotData model,
        DayPilotViewOptions options)
    {
        var calendar = new DayPilotCalendar();
        if (model != null)
        {
            model.CopyTo(calendar);
        }
        if (options != null)
        {
            options.CopyTo(calendar);
        }
        var sb = new System.Text.StringBuilder();
        sb.Append("<div class=\"dayPilot\">"); // allows working around td cellpadding bug in css
        using (var sw = new System.IO.StringWriter(sb))
        {
            using (var tw = new HtmlTextWriter(sw))
            {
                calendar.RenderControl(tw);
            }
        }
        sb.Append("</div>");
        return sb.ToString();
    }

Regarding #2, if it ain't broke...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文