开发者

ASP.NET: Moving ViewState to bottom of page

What are the latest and greatest ways to move ViewState to bottom of the page

Can this be done in a IHttpHandler that can be specified in the web.config to intercept requests to "*.aspx"?

<httpHandlers>
    <add verb="*" path="*.aspx" type="MyApp.OptimizedPageHandler" />
<httpHandlers>

Other options is that this could be done in a IHttpModule, but that is not as performant, as it will intercept all requests.

开发者_JS百科

Also it could be done in an a class deriving from the Page or MasterPage-class, but this is not as modular.

Are there any performance penalties to this?


You can control how and where ViewState data is loaded and saved by creating a custom implementation of the PageStatePersister class. Then create a base class for all your ASPX pages and override the PageStatePersister method to return your custom implementation. This can then tap into whichever page events you want to store the viewstate per your requirements.

I question whether or not it's worthwhile though. Are you storing a ton of data int he ViewState unnecessarily? Maybe you can get more benefit easier by just using ViewState less or turning it off for some controls as opposed to just moving it to a different place within the HTML page.


After conducting some research I put together this blog-post.

I solved the issue by creating a HttpModule and applying a Response Filter, which modifies the output of the page and moves the ViewState to the bottom of the form.

public class ViewStateSeoHttpModule : IHttpModule {
    public void Init(HttpApplication context) {
        context.BeginRequest += new EventHandler(BeginRequest);
    }

    private void BeginRequest(object sender, EventArgs e) {
        HttpApplication application = sender as HttpApplication;

        bool isAspNetPageRequest = GetIsAspNetPageRequest(application);
        if(isAspNetPageRequest) {
            application.Context.Response.Filter =
                new ViewStateSeoFilter(application.Context.Response.Filter);
        }
    }

    private bool GetIsAspNetPageRequest(HttpApplication application) {
        bool isAspNetPageRequest = application.Context.Handler is System.Web.UI.Page;
        return isAspNetPageRequest;
    }
    // [...]


Extending the Page class gives you the most control over page rendering. If you want to move ViewState to the bottom of a given page, use your custom base class for that page. If you don't need to, use the Page class.

The only performance risk when deriving from the Page class is in your implementation of overridden methods. Unless you're doing something particularly inefficient, there should not be any perceptible performance hit.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜