开发者

HttpHandler vs HttpModule for Overriding Render Function on ASP.NET Page

Im trying to find all CSS tags on a generated page

So from what I understand there are two ways of doing this...

  1. Override the function directly on开发者_Python百科 the ASP.NET page and inject one link tag in the HTML referencing some ashx (css.ashx) page (HttpHandler) that will then read all the CSS files in and send them all out together in the response as one. Do this same overriding for all the other pages.

OR

  1. Use an HttpModule which uses the same approach but doesnt need the constant overriding of the Render function on every ASP.NET page.

Whats the advantages/disadvantages of each and what do you guys recommend?

Thanks


I think what you're trying to solve is having a single request for a CSS file rather than multiple requests for all the CSS files.

I don't think either of your solutions are going to work. If I understand what you're doing. If you grab all the requested files and add a single link to handle an aggregated version, how do you decide what is actually put into the handler when the request is made?

A better solution would be to add a single link in your master page to a handler. Then your handler aggregates the css files, compresses them, caches it and outputs it.

Edit: To address the comment: "i minify in the build process...i use IIS compression... the aggregated version looks something like this css.ashx?file1.css,file2.css,someOtherfile.css the files are seperated by commas. And i handle that with logic...Then i can add caching and custom headers in the response.... Get it?"

In my opinion. It seems a bit silly to create that link every page request. It seems unnecessary. The concept is fine and it's something I do (differently) but I don't agree with your approach.

I've seen blog posts which detail the minify on the build process which is fine, but if you need to make a minor change to the css you can't just quickly jump on the server, change a value, and be done with it, you actually need to go through the build process again and upload the single file.

Also I don't think having all your files as parameters is a good idea, you're making a bunch of different urls for every page.

The way I do it is create a single handler, with 2 query string values. A key, and version.

The key is located in the web.config appsettings with a comma separated string of css files to include.

The request to the handler is made, it looks up the key/version in the cache, if it doesn't exist, it grabs the value from the web.config, loads all the files, minifies them, compresses them and caches it. Add's all the headers and outputs the request.

Edit 2: The other way you can do it, is to just have the single link, and have a the combine/minify in the build process. Even if you're combining a few css files not used on the homepage, these should be relatively small, and since they are cached then you're speeding up the request on the second page request.

In your scenario you're requiring the browser to issue a request for css files it already had, PLUS additional css files you may have included in the second page. (if that makes sense)

HomePage - css.ashx?files=css1.css,css2.css,css3.css
AboutPage - css.ashx?files=css1.css,css3.css

This is pointless because you could have reused the css from the homepage to render the about page.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜