csharp 在过滤之前像Rails一样

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 在过滤之前像Rails一样相关的知识,希望对你有一定的参考价值。

public class BeforeAttribute : ActionFilterAttribute {
	public BeforeAttribute() {
		Methods = string.Empty;
		Except  = string.Empty;
		Only    = string.Empty;
	}

	public BeforeAttribute(params string[] methodNames) : this() {
		Methods = string.Join(" ", methodNames);
	}

	public virtual string Methods { get; set; }
	public virtual string Except  { get; set; }
	public virtual string Only    { get; set; }

	public virtual string[] MethodNames   { get { return Methods.Split(' '); } }
	public virtual string[] ExceptActions { get { return Except.Split(' ');  } }
	public virtual string[] OnlyActions   { get { return Only.Split(' ');    } }

	// This is only called if we check Except/Only actions and determine that we should get a result
	public ActionResult GetResult(ActionExecutingContext filterContext) {
		ActionResult result = null;

		var type = filterContext.Controller.GetType();
		foreach (var methodName in MethodNames) {
			var method = type.GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
			result     = method.Invoke(filterContext.Controller, new object[]{}) as ActionResult;
			if (result != null)
				break;
		}

		return result;
	}

	public override void OnActionExecuting(ActionExecutingContext filterContext) {
		var actionName      = filterContext.ActionDescriptor.ActionName;
		ActionResult result = null;
		
		if (OnlyActions.Any())
			if (OnlyActions.Contains(actionName))
				result = GetResult(filterContext);
		else
			if (! ExceptActions.Contains(actionName))
				result = GetResult(filterContext);

		if (result == null)
			base.OnActionExecuting(filterContext);
		else
			filterContext.Result = result;
	}
}

以上是关于csharp 在过滤之前像Rails一样的主要内容,如果未能解决你的问题,请参考以下文章

Rails 5.0.0.beta1 - 从未过滤的请求参数生成 URL 是不安全的

csharp 像Umbraco的API一样的JQuery

在 Rails 中过滤敏感信息(参数)的测试

JSP,能不能像Ruby/Rails/Erb中的yield, layout, content_for一样工作

为啥我不能像使用列表一样过滤 IQueryable?

如何在 Qt TableView 中实现像过滤一样的电子表格?