π RedirectIfUsingInternetExplorer.cs Documentation
Assembly: IrbyImaging.BLUtility
Author: kvamshi
Created: 2022β05β17
Last Modified: 2022β05β17
π Table of Contents
- Overview & Purpose
- Class Diagram (UML)
- Namespaces & Dependencies
- Core Functionality
- Usage & Registration
- Sample Code Blocks
- Usage Examples
- Error Handling & Considerations
- Third-Party Integrations
- 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:
- Read the
User-Agentheader. - Detect the substring
"Trident"(IEβs rendering engine). - Set the
Resultto aRedirectToActionResultβnever executes the original action. - Invoke
base.OnActionExecutingfor 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
-
Global Application
All incoming requests:- IE β
/Tickets/InternetExplorerMessage - Chrome/Firefox/Edge β Normal flow
- IE β
-
Specific Controller
Only hitsHomeControlleractions:[RedirectIfUsingInternetExplorer] public class HomeController : Controller { β¦ } -
Single Action
IE visitors on/Blog/Read/123are redirected; others see the post.
β οΈ Error Handling & Considerations
- Header Absence
IfUser-Agentis missing or empty, the string check returnsfalseand 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 setResult, 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