Welcome to TheCredence.com - You may like to subscribe to our RSS feed to stay updated.
This is one of the last article in the series of Advanced ASP Tutorials. After this I will cover lot of wordpress plugins with there features.
So enjoy writting custom http headers in ASP.
So sometimes web application needs a low level programming in order to achieve a specific purpose. Imagine you need your application do some processing which differs from the web form model. Take a look at the Page class. It is primary focused on managing the user interface of the application. When user call an aspx file the Page class is responsible to represent the UI oriented html. Web services take the user requests which contains a number of packed method calls, unpack them and return the need information in response. As you know all the endpoint for all HTTP requests destined for ASP.NET is a class implementing IHttpHandler.
How Custom Handlers Work
Building custom handlers can easily be accomplished by creating a class that implements IHttpHandler interface. The interface consists of two methods.
public interface IHttpHandler { void ProcessRequest(HttpContext ctx); bool IsReusable {get;} }
First is the main place where all the processing is done. Via the HttpContext coder can obtain the Response, Request and Server objects. Second determines if the handler can be reused again after an object has been processed or should be discarded. I other words once the request finally arrives at the handler ProcessRequest can do something to respond to the request.
Sample
We will build custom HttpHandler which will load sql script files and show them in the browser with colored in blue keywords. Follow the next steps.
1. Create new class and implement interface IHttpHandler like this.
using System; using System.Web; using System.IO; namespace bbb { public class SqlHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; System.IO.FileInfo fi = new FileInfo(context.Request.MapPath(".") + @"\" + Path.GetFileName(context.Request.FilePath)); StreamReader r = fi.OpenText(); String sql = r.ReadToEnd(); r.Close(); sql = sql.Replace("\r\n", @"<br/>"); sql = sql.Replace("SELECT", @"<span style=""color: Blue"">SELECT</span>"); sql = sql.Replace("USE", @"<span style=""color: Blue"">USE</span>"); sql = sql.Replace("GO", @"<span style=""color: Blue"">GO</span>"); sql = sql.Replace("SET", @"<span style=""color: Blue"">SET</span>"); sql = sql.Replace("ON", @"<span style=""color: Blue"">ON</span>"); sql = sql.Replace("ANSI_NULLS", @"<span style=""color: Blue""> ANSI_NULLS</span>"); sql = sql.Replace("QUOTED_IDENTIFIER", @"<span style=""color: Blue""> QUOTED_IDENTIFIER</span>"); sql = sql.Replace("CREATE", @"<span style=""color: Blue"">CREATE</span>"); sql = sql.Replace("PROCEDURE", @"<span style=""color: Blue""> PROCEDURE</span>"); sql = sql.Replace("AS", @"<span style=""color: Blue"">AS</span>"); sql = sql.Replace("FROM", @"<span style=""color: Blue"">FROM</span>"); sql = sql.Replace("WHERE", @"<span style=""color: Blue"">WHERE</span>"); sql = sql.Replace("NOCOUNT", @"<span style=""color: Blue""> NOCOUNT</span>"); context.Response.Write("<html>"); context.Response.Write("<body>"); context.Response.Write(sql); context.Response.Write("<body/>"); context.Response.Write("<body/>"); } public bool IsReusable { get { return true; } } } }
2. Move the class in the App_Code special folder.
3. Register the handler in the web.config file.
<configuration> <system.web> <httpHandlers> <add path="**.sql" verb="*" type="bbb.SqlHandler"/> </httpHandlers> </system.web> </configuration>
4. Add sql script file in the directory of your application and create a link to it on the default page.
<body> <form id="form1" runat="server"> <div> Use this link to load the sql file. <a href="user.sql">link</a> </div> </form> </body>
And here is the result on the browser:
USE [UserDb] GO /****** Object: StoredProcedure [dbo].[GetUsersWithArticleResult] Script Date: 09/06/2007 17:25:04 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROCEDURE [dbo].[GetUsersWithArticleResult] AS /* SET NOCOUNT ON */ SELECT * FROM [User] WHERE article_result>0
In the real world in order this handler to work when web application is hosted on IIS you should do a little more work. There are two variants. First to register the .sql extension on the IIS:

The second is to create a generic HttpHandler - .ashx file which is registered to IIS and put the above code in it.
ASPLinks you may find interesting -

