是否有可能使ViewState中的自定义实现? [英] Is it possible to make a custom implementation of ViewState?

查看:148
本文介绍了是否有可能使ViewState中的自定义实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Asp.Net有一些选项来影响的方式产生它的ViewState的页面(加密,增加了MAC,ViewStateUserKey的)。

Asp.Net has some options to influence the ways a page its ViewState is generated (encryption, adding of a mac, ViewStateUserKey).

我想自己做,不是基于配置,但对我自己的类,用于序列化和加密等算法。这可能吗?

I would like to do it myself, not based on configuration, but on my own class that used other algorithms for serialization and encryption. Is this possible?

推荐答案

是的,这是可能的。例如,我建立了基于一些文章,你可以找到$ C $的CProject视图状态COM pression逻辑。你需要重写 PageStatePersister ,并创建 PageStatePersister

Yes, it's possible. For instance, I built a view state compression logic based on some articles you can find on CodeProject. You'll need to override PageStatePersister from Page and create a class derivated from PageStatePersister:

// In your page:
protected override PageStatePersister PageStatePersister
{
   get { return new ViewStateCompressor(this); }
}

和创建一个新类:

public class ViewStateCompressor : PageStatePersister
{
    private const string ViewStateKey   = "__VSTATE";
    public ViewStateCompressor(Page page) : base(page)
    {
    }

    private LosFormatter stateFormatter;
    protected new LosFormatter StateFormatter
    {
        get { return this.stateFormatter ?? (this.stateFormatter = new LosFormatter()); }
    }

    public override void Save()
    {
        using (StringWriter writer = new StringWriter(System.Globalization.CultureInfo.InvariantCulture))
        {
            // Put viewstate data on writer
            StateFormatter.Serialize(writer, new Pair(base.ViewState, base.ControlState));

            // Handle your viewstate data
            // byte[] bytes = Convert.FromBase64String(writer.ToString());

            // Here I create another hidden field named "__VSTATE"
            ScriptManager.RegisterHiddenField(Page, ViewStateKey, Convert.ToBase64String(output.ToArray()));
        }
    }

    public override void Load()
    {
        byte[] bytes = Convert.FromBase64String(base.Page.Request.Form[ViewStateKey]);
        using (MemoryStream input = new MemoryStream())
        {
            input.Write(bytes, 0, bytes.Length);
            input.Position = 0;

            // Handle your viewstate data

            Pair p = ((Pair)(StateFormatter.Deserialize(Convert.ToBase64String(output.ToArray()))));
            base.ViewState = p.First;
            base.ControlState = p.Second;
        }
    }
}

这篇关于是否有可能使ViewState中的自定义实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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