πŸš€ RedirectIfUsingInternetExplorer.cs Documentation

Assembly: IrbyImaging.BLUtility
Author: kvamshi
Created: 2022‑05‑17
Last Modified: 2022‑05‑17


πŸ“‘ Table of Contents

  1. Overview & Purpose
  2. Class Diagram (UML)
  3. Namespaces & Dependencies
  4. Core Functionality
  5. Usage & Registration
  6. Sample Code Blocks
  7. Usage Examples
  8. Error Handling & Considerations
  9. Third-Party Integrations
  10. Conclusion

πŸ” Overview & Purpose

RedirectIfUsingInternetExplorer is an ASP.NET Core Action Filter that detects if an incoming HTTP request originates from Microsoft Internet Explorer (IE).
If IE is detected (via the Trident token in the User-Agent header), the filter redirects the user to a dedicated β€œInternet Explorer” message page.

Why?

  • Internet Explorer is no longer fully supported by many modern web frameworks.
  • Provide users with a clear message or upgrade path rather than a broken UI.

πŸ“ˆ Class Diagram (UML)

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚       ActionFilterAttribute   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
               β”‚
               β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ RedirectIfUsingInternetExplorer β”‚
β”‚  Β«inheritsΒ» ActionFilterAttribute β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

🧩 Namespaces & Dependencies

Dependency Purpose
Microsoft.AspNetCore.Mvc Core MVC classes (Controller, Filters).
Microsoft.AspNetCore.Mvc.Filters Provides ActionFilterAttribute.
Microsoft.Net.Http.Headers Standardized HTTP header names (e.g., User-Agent).

πŸ”§ Core Functionality

4.1 OnActionExecuting Method

public override void OnActionExecuting(ActionExecutingContext context)
{
    // 1. Read the User-Agent header from the request
    var userAgent = context.HttpContext.Request
                   .Headers[HeaderNames.UserAgent].ToString();

    // 2. Check for the "Trident" engine (Internet Explorer)
    if (userAgent.Contains("Trident"))
    {
        // 3. Redirect to TicketsController.InternetExplorerMessage
        context.Result = new RedirectToActionResult(
                             actionName: "InternetExplorerMessage",
                             controllerName: "Tickets",
                             routeValues: null
                         );
    }

    // 4. Call the base implementation (optional for chaining)
    base.OnActionExecuting(context);
}

Step-by-step:

  1. Read the User-Agent header.
  2. Detect the substring "Trident" (IE’s rendering engine).
  3. Set the Result to a RedirectToActionResultβ€”never executes the original action.
  4. Invoke base.OnActionExecuting for proper filter chaining.

πŸ“Œ Usage & Registration

5.1 Global Filter Registration (Startup.cs)

To apply this filter globally to every MVC action:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews(options =>
    {
        options.Filters.Add<RedirectIfUsingInternetExplorer>();
    });
}

5.2 Controller/Action Attribute Usage

To apply selectively, decorate a controller or action:

using IrbyImaging.Utility;

[RedirectIfUsingInternetExplorer]
public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
}

Or on a single action:

public class BlogController : Controller
{
    [RedirectIfUsingInternetExplorer]
    public IActionResult Read(int id)
    {
        // IE users get redirected; others see post.
        return View();
    }
}

πŸ“š Sample Code Blocks

RedirectIfUsingInternetExplorer.cs

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Net.Http.Headers;

namespace IrbyImaging.Utility
{
    public class RedirectIfUsingInternetExplorer : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            var userAgent = context.HttpContext.Request.Headers[HeaderNames.UserAgent]
                                .ToString();

            if (userAgent.Contains("Trident"))
            {
                context.Result = new RedirectToActionResult(
                    "InternetExplorerMessage",
                    "Tickets",
                    null
                );
            }

            base.OnActionExecuting(context);
        }
    }
}

TicketsController.cs (Target of Redirect)

public class TicketsController : Controller
{
    public IActionResult InternetExplorerMessage()
    {
        // You can render a special view explaining that IE is unsupported
        return View();
    }
}

πŸ” Usage Examples

  1. Global Application
    All incoming requests:

    • IE β†’ /Tickets/InternetExplorerMessage
    • Chrome/Firefox/Edge β†’ Normal flow
  2. Specific Controller
    Only hits HomeController actions:

    [RedirectIfUsingInternetExplorer]
    public class HomeController : Controller { … }
    
  3. Single Action
    IE visitors on /Blog/Read/123 are redirected; others see the post.


⚠️ Error Handling & Considerations

  • Header Absence
    If User-Agent is missing or empty, the string check returns false and request proceeds.
  • False Positives
    Any custom UA containing "Trident" will be redirected. Adjust to match more patterns if needed.
  • Performance
    Simple string checkβ€”minimal overhead.
  • Chain of Filters
    If multiple filters set Result, the first one in the pipeline wins.

πŸ”— Third-Party Integrations

  • Uses Microsoft.AspNetCore.Mvc and Microsoft.Net.Http.Headers from ASP.NET Core.
  • No external NuGet packages required beyond standard ASP.NET Core dependencies.

🏁 Conclusion

The RedirectIfUsingInternetExplorer filter is a lightweight, reusable utility to:

  • Gracefully notify IE users that your site no longer supports their browser.
  • Keep code DRY by centralizing the redirect logic.
  • Customize the redirect target (controller/action) as your messaging evolves.

Apply it globally or locally, and keep your application experience modern and consistent! πŸŽ‰


End of Documentation