1

i am trying to compress the webpages of my website to increase its speed. i am done with JS and CSS compression. now i want to compress my aspx pages before the response is out. i am using this code in global.asax file of my website

void Application_Start(object sender, EventArgs e)
{
    HttpContext incoming = HttpContext.Current;
    string oldpath = incoming.Request.Path.ToLower();
    incoming.Response.Filter = new System.IO.Compression.GZipStream(incoming.Response.Filter, System.IO.Compression.CompressionMode.Compress);
    HttpContext.Current.Response.AppendHeader("Content-encoding", "gzip");
    HttpContext.Current.Response.Cache.VaryByHeaders["Accept-encoding"] = true;
}

it donot gives error in visual studio. but when i put this code on IIS, it gives error/exception

Exception Details: System.Web.HttpException: Request is not available in this context

can anyone suggest or explain what should i do.

Dolly Dar
  • 97
  • 1
  • 12

2 Answers2

0

Application_Start is executed when your web application starts and this start is not associated with any page request, as page request is not happened yet at this point. Request is not available on Application_Start.

You can use Server.MapPath() instead.

akhil
  • 1,202
  • 3
  • 21
  • 43
0

The issue here is an elusive one, the built in Visual Studio cassini web server will be running requests using the older pattern of firing up the application upon the first request, which is the same as Managed Pipeline mode = classic in IIS. This means that there is a request object for you to access straight away, as the request is what triggered the app_start.

However, when you put this onto an IIS 7 box with a Managed pipeline mode = Integrated, it will fail. This is because an integrated pipe means that the site is started as soon as the app pool fires up, meaning there is no request object for it to hook into.

To solve this problem I'd recommend letting IIS compress the content rather than doing it by hand, this link has the details to get you started and here is a good outline about the difference it can make.

If you're really determined to do the compression within the application, I'd suggest implementing it as a HttpModule, similar to this example.

EDIT: Another implementation of a gzipping HttpModule here.

JonVD
  • 4,258
  • 24
  • 24
  • actully i am using rewriting module in IIS. And i am not able to enable dynamic compression as true because it gives error. but i need to do compression of html generated from aspx page. :( – Dolly Dar Jul 06 '12 at 10:39
  • That's fine, I frequently use my own compression within asp.net, the only difference is that I'm using MVC where I can simply put it as an ActionFilter. The problem that you're going to have is that you can't re-write the url when you've compressed the content (it would need to uncompress it, re-write it and then recompress). That means that the compression *must* take place after the re-write in order to work correctly. http://forums.iis.net/t/1165899.aspx <- this post seems to have addressed this by re-ordering the module execution to do just that. Let me know how you go. – JonVD Jul 08 '12 at 23:35