加载时替换aspx页面中的令牌 [英] Replace tokens in an aspx page on load

查看:85
本文介绍了加载时替换aspx页面中的令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个aspx页面,其中包含常规html,一些uicomponents和{tokenname}形式的多个标记。

I have an aspx page that contains regular html, some uicomponents, and multiple tokens of the form {tokenname} .

该页面加载后,我想解析页面内容,并用正确的内容替换这些标记。这个想法是,后面会有多个使用相同代码的模板页面。

When the page loads, I want to parse the page content and replace these tokens with the correct content. The idea is that there will be multiple template pages using the same codebehind.

我毫不费力地解析字符串数据本身(请参见命名字符串格式在模板中替换令牌),我的麻烦在于何时阅读以及如何阅读将数据写回页面...

I've no trouble parsing the string data itself, (see named string formatting, replace tokens in template) my trouble lies in when to read, and how to write the data back to the page...

对我来说,重写页面内容的最佳方法是什么?我一直在使用流阅读器,并用Response.Write替换页面,但这不好-包含其他.net组件的页面无法正确呈现。

What's the best way for me to rewrite the page content? I've been using a streamreader, and the replacing the page with Response.Write, but this is no good - a page containing other .net components does not render correctly.

任何建议将不胜感激!

推荐答案

非常感谢促成此问题的人员,但是我最终使用了其他方法解决方案-

Many thanks to those that contributed to this question, however I ended up using a different solution -

根据此页面,除了我使用正则表达式解析了多个不同标签的页面内容。

Overriding the render function as per this page, except I parsed the page content for multiple different tags using regular expressions.

protected override void Render(HtmlTextWriter writer)
    {
         if (!Page.IsPostBack)
        {
            using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
            {
                using (System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(stream))
                {
                    HtmlTextWriter htmlWriter = new HtmlTextWriter(streamWriter);
                    base.Render(htmlWriter);
                    htmlWriter.Flush();
                    stream.Position = 0;
                    using (System.IO.StreamReader oReader = new System.IO.StreamReader(stream))
                    {
                        string pageContent = oReader.ReadToEnd();
                        pageContent = ParseTagsFromPage(pageContent);
                        writer.Write(pageContent);
                        oReader.Close();
                    }
                }
            }
        }
        else
        {
            base.Render(writer);
        }
    }

这里是regex标签解析器

Here's the regex tag parser

private string ParseTagsFromPage(string pageContent)
    {
        string regexPattern = "{zeus:(.*?)}"; //matches {zeus:anytagname}
        string tagName = "";
        string fieldName = "";
        string replacement = "";
        MatchCollection tagMatches = Regex.Matches(pageContent, regexPattern);
        foreach (Match match in tagMatches)
        {
            tagName = match.ToString();
            fieldName = tagName.Replace("{zeus:", "").Replace("}", "");
            //get data based on my found field name, using some other function call
            replacement = GetFieldValue(fieldName); 
            pageContent = pageContent.Replace(tagName, replacement);
        }
        return pageContent;
    }

似乎工作得很好,因为在GetFieldValue函数中可以使用字段以您希望的任何方式命名。

Seems to work quite well, as within the GetFieldValue function you can use your field name in any way you wish.

这篇关于加载时替换aspx页面中的令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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