public void OnActionExecuting(ActionExecutingContext context)
{
var referer = context.HttpContext.Request.Headers["Referer"].ToString();
_logger.LogInformation($"Referer: {referer}");
if (!string.IsNullOrEmpty(referer))
{
if (referer.Contains("r10.net"))
{
_logger.LogInformation("Redirecting to /Home/Index");
context.Result = new RedirectToActionResult("Refferrer", "Home", null);
}
else if (referer.Contains("erdemcelebi.com"))
{
_logger.LogInformation("Redirecting to /Home/Refferrer");
context.Result = new RedirectToActionResult("Index", "Home", null);
}
}
} .net core mvc c# dışarıdan gelen linki yönlendirme hk
1
●116
- 12-06-2024, 21:06:41Merhaba arkadaşlar, bir tane .net core mvc web projem var *.r10.net üzerinden gelen kullanıcılar için index2 sayfası açılmasını istiyorum diğer gelen ziyaretler için index'e normal açılmasını istiyorum bu karşılamayı yapamadım yada daha sağlıklı bu yönlendirmeyi nasıl yapabilirim
- 27-07-2024, 23:15:59
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>(); });