Tuesday, December 4, 2012

Custom HTTP Handler 2


===================
KompasPageHandler2
===================

using System.Web;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System;

/// <summary>
/// Summary description for KompasPageHandler
/// </summary>
namespace Kompas
{

    public class KompasPageHandler2 : IHttpHandler
    {
        public KompasPageHandler2()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            string urlPath = context.Request.RawUrl;

            string[] splitUrl = urlPath.Split('/');
            context.Response.Write(context.Request.RawUrl);
            context.Response.Write("<BR/>");
            context.Response.Write(
                String.Format("Tahun {0} Bulan {1}, Artikel {2}",
                    splitUrl[3], splitUrl[4], splitUrl[5]
                )
                //"Tahun " + splitUrl[1]
            );
           
            for (int i = 0; i < splitUrl.Length; i++)
            {
                context.Response.Write(splitUrl[i] + "<BR/>");
            }
            //context.Response.Write("Halooo...");

        }
    }
}

===================
web.config
===================

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <httpHandlers>
      <add path="*/kompas" verb="*" type="Kompas.KompasPageHandler" />
      <add path="kompas2/*/*/*" verb="*" type="Kompas.KompasPageHandler2" />
    </httpHandlers>
    <compilation debug="true" targetFramework="4.0"/>
    <urlMappings>
      <add url="~/gambarsaja" mappedUrl="~/Gambar.ashx"/>
    </urlMappings>
  </system.web>
</configuration>

No comments:

Post a Comment