Friday, September 28, 2007

HTTP Compression on IIS 6 Server


1. First enable compression on iis server, Right click on Web Sites, Properties.
Select Services Tab

2. Add gzip as server extention, Right Click on Web Service Extensions. Add a new Web Service Extensions. Add Following file
C:\WINDOWS\system32\inetsrv\gzip.dll




Thursday, September 27, 2007

C# HttpResponse.Filter Property and http commression

You can use that property to modify the HTTP entity body before transmission to the client browser.

If you want to send the http response compressed for example.

First you should determine whether client browser accepts compressed content.

Browsers send this information in http header value Accept-encoding.

if(Request.Headers["Accept-encoding"] != null &&
Request.Headers["Accept-encoding"].Contains("gzip"))
{
}

or

if(Request.Headers["Accept-encoding"] != null &&
Request.Headers["Accept-encoding"].Contains("deflate"))
{
}

Here is the complete code. You can put it in you base page, in OnInit or OnLoad methods for example
if(Request.Headers["Accept-encoding"] != null)
{
if (
Request.Headers["Accept-encoding"].Contains("deflate"))
{
Response.Filter = new DeflateStream(Response.Filter, CompressionMode.Compress, true);
Response.AppendHeader("Content-encoding", "deflate"); }
else if (Request.Headers["Accept-encoding"].Contains("deflate"))
{
Response.Filter = new GZipStream(Response.Filter, CompressionMode.Compress, true);
Response.AppendHeader("Content-encoding", "gzip");
}
}


Tuesday, September 25, 2007

Rewrite urls in asp.net (To make them search engine friendly)

Suppose you have urls that looks as that one

http://www.vic.bg/Default.aspx?c=29
http://www.vic.bg/Default.aspx?c=30
http://www.vic.bg/Default.aspx?c=31

but you want to looks like those:

http://www.vic.bg/Police.aspx
http://www.vic.bg/Students.aspx
http://www.vic.bg/Sport.aspx

without having actual pages.

You should add new IHttpHandler in your web.config file which dynamically will transform from one to another.

Step by step:

1. Add new library project.
2. Add new class and inherit PageHandlerFactory. That is .net handler that process .aspx files. We will add logic to rewrite pages and then will pass them to original handler

You can add much complex logic here for example transform one url to another with regular expression.


namespace VicHttpHandlers
{
public class VicRewriteUrlHandlerFactory : PageHandlerFactory
{
VirtualPathTableAdapter vpta = new VirtualPathTableAdapter();

Dictionary _pathMaps = new Dictionary();

public VicRewriteUrlHandlerFactory()
{
Vicove.VirtualPathDataTable dt = vpta.GetData();
foreach (Vicove.VirtualPathRow row in dt)
_pathMaps.Add(row.VirtualPath, row.RealPath);
}
#region IHttpHandler Members
VicRewriteUrlHandler handler = new VicRewriteUrlHandler();

public override IHttpHandler GetHandler(
HttpContext context,
string requestType,
string virtualPath,
string path)
{
if (_pathMaps.ContainsKey(HttpUtility.UrlDecode(context.Request.Url.PathAndQuery)))
{
context.RewritePath(_pathMaps[HttpUtility.UrlDecode(context.Request.Url.PathAndQuery)], false);
virtualPath = context.Request.Path;
path = context.Request.PhysicalPath;
}

return base.GetHandler(context, requestType, virtualPath, path);

}

#endregion
}
}

4. Change you web.config

<httpHandlers>
<add verb="*" path="*.aspx" type="VicHttpHandlers.VicRewriteUrlHandlerFactory, VicHttpHandlers" />