自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

ASP.NET Core 判斷請求是否為Ajax請求

開發(fā) 后端
在寫后臺程序時,有時候需要知道客戶端發(fā)送的是普通的請求,還是ajax 請求,最近在做項(xiàng)目的時候,有些地方需要判斷當(dāng)前的請求是不是ajax。

 [[438014]]

本文轉(zhuǎn)載自微信公眾號「UP技術(shù)控」,作者conan5566。轉(zhuǎn)載本文請聯(lián)系UP技術(shù)控公眾號。

概述

在寫后臺程序時,有時候需要知道客戶端發(fā)送的是普通的請求,還是ajax 請求,最近在做項(xiàng)目的時候,有些地方需要判斷當(dāng)前的請求是不是ajax。特地找了下發(fā)現(xiàn),jQuery 發(fā)出 ajax 請求時,會在請求頭部添加一個名為 X-Requested-With 的信息,信息內(nèi)容為:XMLHttpRequest。Ajax請求的request headers里都會有一個key為x-requested-with,值為XMLHttpRequest的header,所以我們就可以使用這個特性進(jìn)行判斷。

判斷是不是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.     } 

 

 

責(zé)任編輯:武曉燕 來源: UP技術(shù)控
相關(guān)推薦

2009-07-28 15:29:03

實(shí)現(xiàn)HTTP請求ASP.NET

2009-07-24 13:41:15

ASP.NET AJA

2009-07-22 16:17:39

ASP.NET AJA

2009-07-22 16:11:43

ASP.NET AJA

2009-07-22 16:25:41

ASP.NET AJA

2009-07-22 16:05:34

ASP.NET AJA

2009-06-24 09:12:26

ASP.NET頁面請求

2024-05-13 09:32:06

攔截器HTTP中間件

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-08-07 16:09:25

ASP.NET AJA

2009-07-20 17:39:36

WCF服務(wù)ASP.NET AJA

2009-07-20 13:54:31

ScriptManagASP.NET AJA

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
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號