• Decrease Text SizeIncrease Text Size

How to setup Custom HttpModule

Posted Date: 5/9/2023
    Printer Friendly Version   Email A Friend   Add This   Increase Text Size   Decrease Text Size
Sometimes you need to execute your code using early in the page lifecycle then you can use HttpModules

add to module web.config


 <add name="HttpCustomRequest" preCondition="managedHandler" type="Centralpoint.WebSite.HttpModules.Custom.HttpCustomRequest" />



Then copy the code into HttpModules.cs file and move that file into Root/App_code/Custom directory






using System;
using System.Collections.Generic;
using System.Web;

namespace Centralpoint.WebSite.HttpModules.Custom
{
    public class HttpCustomRequest : IHttpModule
    {
        public void Init(HttpApplication application)
        {
            application.BeginRequest += new EventHandler(this.OnBeginRequest);
        }

        public void Dispose()
        {
        }

        public void OnBeginRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            HttpContext context = application.Context;
            string url = context.Request.Url.ToString();

            if(!url.StartsWith("http://245.643.136.178") || !url.StartsWith("https://255.143.136.178"))
                return;

            HttpContext.Current.Trace.Warn("application.Context.Request", $"URL: {url}  Path: {context.Request.Path}");
            //HttpContext.Current.Trace.Warn("context.Request.Url.Host", $" {context.Request.Url.Host}");
            //HttpContext.Current.Trace.Warn("context.Request.Url.Authority", $" {context.Request.Url.Authority}");
            //HttpContext.Current.Trace.Warn("context.Request.Url.AbsolutePath", $" {context.Request.Url.AbsolutePath}");
            //HttpContext.Current.Trace.Warn("context.Request.Url.Scheme", $" {context.Request.Url.Scheme}");
            switch (url.ToLower())
            {
                case "http://155.643.136.178/shc":
                case "https://194.143.136.178/shc":
                    HttpContext.Current.Trace.Warn("Custom.Module.OnBeginRequest", "redirect");
                    context.Response.Redirect("https://myWebsite.org/shc", true);
                    break;
                case "http://155.643.136.178/e-w":
                case "https://194.143.136.178/e-w":
                    HttpContext.Current.Trace.Warn("Custom.Module.OnBeginRequest", "redirect");
                    context.Response.Redirect("https://myWebsite.com", true);
                    break;
                case "http://155.643.136.178/pga-golf":
                case "https://194.143.136.178/pga-golf":
                    HttpContext.Current.Trace.Warn("Custom.Module.OnBeginRequest", "redirect");
                    context.Response.Redirect("https://myWebsite.com", true);
                    break;
                default:
                    HttpContext.Current.Trace.Warn("Custom.Module.OnBeginRequest", $"Not trackable URL: {url}");
                    break;
            }
        }
    }
}




Keywords: HttpModule



Related Taxonomy
  - How Do I?
  - Questions

Comments:

Be the first to leave a comment.
Please Login to post comments.