ASP.NET Core 判断请求是否为Ajax请求

开发 后端
在写后台程序时,有时候需要知道客户端发送的是普通的请求,还是ajax 请求,最近在做项目的时候,有些地方需要判断当前的请求是不是ajax。

 [[438014]]

本文转载自微信公众号「UP技术控」,作者conan5566。转载本文请联系UP技术控公众号。

概述

在写后台程序时,有时候需要知道客户端发送的是普通的请求,还是ajax 请求,最近在做项目的时候,有些地方需要判断当前的请求是不是ajax。特地找了下发现,jQuery 发出 ajax 请求时,会在请求头部添加一个名为 X-Requested-With 的信息,信息内容为:XMLHttpRequest。Ajax请求的request headers里都会有一个key为x-requested-with,值为XMLHttpRequest的header,所以我们就可以使用这个特性进行判断。

判断是不是ajax

  1. using System; 
  2.  
  3. namespace CompanyName.ProjectName.Web.Host.Framework 
  4.     public static class RequestExt 
  5.     { 
  6.         /// <summary> 
  7.         /// Determines whether the specified HTTP request is an AJAX request. 
  8.         /// </summary> 
  9.         /// 
  10.         /// <returns
  11.         /// true if the specified HTTP request is an AJAX request; otherwise, false
  12.         /// </returns
  13.         /// <param name="request">The HTTP request.</param> 
  14.         ///  <exception cref="T:System.ArgumentNullException"
  15.         ///  The <paramref name="request"/> 
  16.         ///  parameter is null (Nothing in Visual Basic).</exception> 
  17.         public static bool IsAjaxRequest(this Microsoft.AspNetCore.Http.HttpRequest request) 
  18.         { 
  19.             if (request == null
  20.                 throw new ArgumentNullException("request"); 
  21.  
  22.             if (request.Headers != null
  23.                 return request.Headers["X-Requested-With"] == "XMLHttpRequest"
  24.             return false
  25.         } 
  26.     } 

控制ajax才能使用方法

  1. using Microsoft.AspNetCore.Mvc.Abstractions; 
  2. using Microsoft.AspNetCore.Mvc.ActionConstraints; 
  3. using Microsoft.AspNetCore.Routing; 
  4.  
  5. namespace CompanyName.ProjectName.Web.Host.Framework 
  6.     public class AjaxOnlyAttribute : ActionMethodSelectorAttribute 
  7.     { 
  8.         public bool Ignore { get; set; } 
  9.  
  10.         public AjaxOnlyAttribute(bool ignore = false
  11.         { 
  12.             Ignore = ignore
  13.         } 
  14.  
  15.         public override bool IsValidForRequest(RouteContext routeContext, ActionDescriptor action
  16.         { 
  17.             if (Ignore
  18.                 return true
  19.  
  20.             var request = routeContext.HttpContext.Request; 
  21.             if (request != null && request.Headers != null && request.Headers["X-Requested-With"] == "XMLHttpRequest"
  22.                 return true
  23.  
  24.             return false
  25.         } 
  26.     } 

 

 

责任编辑:武晓燕 来源: UP技术控
相关推荐

2009-07-28 15:29:03

实现HTTP请求ASP.NET

2009-06-24 09:12:26

ASP.NET页面请求

2009-07-22 16:11:43

ASP.NET AJA

2009-07-24 13:41:15

ASP.NET AJA

2009-07-22 16:25:41

ASP.NET AJA

2009-07-22 16:17:39

ASP.NET AJA

2009-07-22 16:05:34

ASP.NET AJA

2009-07-22 15:58:52

ASP.NET AJA

2009-07-31 13:24:43

ASP.NET AJA

2009-07-20 10:16:13

配置ASP.NET A

2009-07-29 13:50:26

UpdatePanelASP.NET

2009-07-28 09:02:32

asp.net aja

2009-10-15 14:50:34

ASP.NET Rou

2009-07-29 15:53:22

ASP.NET AJA

2009-07-20 13:14:25

安装ASP.NET A

2009-07-21 17:18:26

UpdateProgrASP.NET AJA

2011-02-13 09:37:55

ASP.NET

2009-07-20 13:54:31

ScriptManagASP.NET AJA

2009-08-07 16:09:25

ASP.NET AJA

2009-07-20 17:39:36

WCF服务ASP.NET AJA
点赞
收藏

51CTO技术栈公众号