添加项目文件。
This commit is contained in:
34
HT.Cloud.Code/Attribute/HandlerAjaxOnlyAttribute.cs
Normal file
34
HT.Cloud.Code/Attribute/HandlerAjaxOnlyAttribute.cs
Normal file
@ -0,0 +1,34 @@
|
||||
using Microsoft.AspNetCore.Mvc.Abstractions;
|
||||
using Microsoft.AspNetCore.Mvc.ActionConstraints;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
using System;
|
||||
|
||||
namespace HT.Cloud.Code
|
||||
{
|
||||
/// <summary>
|
||||
/// ajax验证
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public class HandlerAjaxOnlyAttribute : ActionMethodSelectorAttribute
|
||||
{
|
||||
public bool Ignore { get; set; }
|
||||
|
||||
public HandlerAjaxOnlyAttribute(bool ignore = false)
|
||||
{
|
||||
Ignore = ignore;
|
||||
}
|
||||
|
||||
public override bool IsValidForRequest(RouteContext routeContext, ActionDescriptor action)
|
||||
{
|
||||
if (Ignore)
|
||||
return true;
|
||||
bool result = false;
|
||||
var xreq = routeContext.HttpContext.Request.Headers.ContainsKey("x-requested-with");
|
||||
if (xreq)
|
||||
{
|
||||
result = routeContext.HttpContext.Request.Headers.ContainsKey("x-requested-with");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user