public class ReferrerRedirectAttribute : ActionFilterAttribute
{
private readonly ILogger<ReferrerRedirectAttribute> _logger; public ReferrerRedirectAttribute(ILogger<ReferrerRedirectAttribute> logger)
{
_logger = logger;
}
public override void OnActionExecuting(ActionExecutingContext context)
{
var referer = context.HttpContext.Request.Headers["Referer"].ToString();
_logger.LogInformation($"Referer: {referer}");
if (!string.IsNullOrEmpty(referer))
{
var uri = new Uri(referer);
if (uri.Host.EndsWith("r10.net", StringComparison.OrdinalIgnoreCase))
{
_logger.LogInformation("Redirecting to /Home/Index2");
context.Result = new RedirectToActionResult("Index2", "Home", null);
}
// Diğer özel yönlendirmeler buraya eklenebilir
}
// Eğer referer boşsa veya özel bir durum yoksa, varsayılan davranışı koruyoruz
base.OnActionExecuting(context);
}
}Bu filtreyi Startup.cs veya Program.cs dosyanızda servis olarak ekleyin:
services.AddScoped<ReferrerRedirectAttribute>();
Controller'ınızda veya spesifik action'larda bu filtreyi kullanın:
[ServiceFilter(typeof(ReferrerRedirectAttribute))]
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
public IActionResult Index2()
{
return View();
}
}tüm controller'larda bu filtreyi kullanmak isterseniz, global olarak da ekleyebilirsiniz:
services.AddControllersWithViews(options =>
{
options.Filters.Add<ReferrerRedirectAttribute>();
});