AjaxOnly Attribute On Dotnet Core
04 Eylül 2019
1 dk okuma süresi
239 okunma
MVC 5 ve öncesinde kullandığımız Request.IsAjaxRequest() metodu malesef dotnet core tarafında yok. Ama bunun pratikliğini yine esnek bir yapı ile bize ActionMethodSelector attribute'ü sağlıyor.
Aşağıdaki kod parçası ile gelen request'in ajax request olup olmadığını anlayabiliyoruz.
public sealed class AjaxOnly : ActionMethodSelectorAttribute { public override bool IsValidForRequest(RouteContext routeContext, ActionDescriptor action) { if (routeContext.HttpContext.Request.Headers != null && routeContext.HttpContext.Request.Headers.ContainsKey("X-Requested-With") && routeContext.HttpContext.Request.Headers.TryGetValue("X-Requested-With", out StringValues requestedWithHeader)) { if (requestedWithHeader.Contains("XMLHttpRequest")) { return true; } } return false; } }
Yazılarıma abone olmak ister misiniz?
İlginizi çekebilecek diğer yazılar.
Webapi "Get exception Method not found: 'System.Collections.ObjectModel.Collection`1