mvc 方法只允许ajax访问

Posted UP技术控

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mvc 方法只允许ajax访问相关的知识,希望对你有一定的参考价值。

有时候我们写一些方法 只想在ajax使用  其他的不想暴露  就可以对方法进行限制

如下:

 

 

[AttributeUsage(AttributeTargets.Method)]
public class HandlerAjaxOnlyAttribute : ActionMethodSelectorAttribute
{
public bool Ignore { get; set; }
public HandlerAjaxOnlyAttribute(bool ignore = false)
{
Ignore = ignore;
}
public override bool IsValidForRequest(ControllerContext controllerContext, System.Reflection.MethodInfo methodInfo)
{
if (Ignore)
return true;
return controllerContext.RequestContext.HttpContext.Request.IsAjaxRequest();
}
}

 

调用方法  如下

[HandlerAjaxOnly]
public ActionResult Updatehttime(string BillNo, string htStartTime, string htendTime, int totalSplitNum)


以上是关于mvc 方法只允许ajax访问的主要内容,如果未能解决你的问题,请参考以下文章