protected override void Render(HtmlTextWriter writer)
{
if (HttpContext.Current.Items["VirtualURL"] != null)
{
string sVirURL = HttpContext.Current.Items["VirtualURL"].ToString();
RewriteFormHtmlTextWriter oWriter = new RewriteFormHtmlTextWriter(writer, sVirURL);
base.Render(oWriter);
}
}HttpContext.Current.Items["VirtualURL"]在什么情况下是有值的?如果这样获得不了,那应该怎么获得?
{
if (HttpContext.Current.Items["VirtualURL"] != null)
{
string sVirURL = HttpContext.Current.Items["VirtualURL"].ToString();
RewriteFormHtmlTextWriter oWriter = new RewriteFormHtmlTextWriter(writer, sVirURL);
base.Render(oWriter);
}
}HttpContext.Current.Items["VirtualURL"]在什么情况下是有值的?如果这样获得不了,那应该怎么获得?
术语是:当前请求的上下文。就是表示一个请求的周期中的一个存储空间,这么理解就好了。
Create a new C# Class Library project in Visual Studio.NET and name it "MyHandler". Visual Studio.NET will automatically add a class named "Class1.cs" into the project. Rename it "NewHandler"; open this class in the code window and change the class name and constructor name to "NewHandler".
The following is the code for the NewHandler class. using System;
using System.Web;namespace MyHandler
{
/**//// <summary>
/// Summary description for NewHandler.
/// </summary>
public class NewHandler : IHttpHandler
{
public NewHandler()
{
//
// TODO: Add constructor logic here
//
} Implementation of IHttpHandler#region Implementation of IHttpHandler
public void ProcessRequest(System.Web.HttpContext context)
{
HttpResponse objResponse = context.Response ;
objResponse.Write("<html><body><h1>Hello 15Seconds Reader ") ;
objResponse.Write("</body></html>") ;
} public bool IsReusable
{
get
{
return true;
}
}
#endregion
}
}
As you can see in the ProcessRequest method, the HTTP handler has access to all ASP.NET intrinsic objects passed to it in its parameter through the System.Web.HttpContext object. Implementing the ProcessRequest method is simply extracting the HttpResponse object from the context object and then sending some HTML out to the client. Similarly, IsReusable returns true to designate that this handler can be reused for processing the other HTTP requests.
Let's compile it and place it in the bin directory of the webapp virtual directory. Step 2
Register this handler by adding the following text in the web.config file: <httpHandlers>
<add verb="*" path="*.15seconds" type="MyHandler.NewHandler,MyHandler"/>
</httpHandlers>Step 3
Since we are creating a handler for handling files of a new extension, we also need to tell IIS about this extension and map it to ASP.NET. If we don't perform this step and try to access the Hello.15seconds file, IIS will simply return the file rather than pass it to ASP.NET runtime. As a consequence, the HTTP handler will not be called.
Launch the Internet Services Manager tool, right click on Default Web Site, select Properties, go to Home Directory tab and press Configuration button. This will popup Application Configuration dialog. Click Add button and fill the Executable field with the path to the aspnet_isapi.dll file and fill .15seconds in the Extension field. Leave the other fields as is; the dialog box should look as follows: Now we are good to go. Launch Internet Explorer and go to the following url: http://localhost/webapp/hello.15seconds You should see the following page:
http://www.codeproject.com/KB/ISAPI/URLRewriting.aspx