我为应用程序日志编写了此代码(customHandle)。但是,我不想在某些操作上运行此代码。
customhandle.cs:
public class CustomHandle: ActionFilterAttribute
{
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
var controllerName = (string)filterContext.RouteData.Values["controller"];
var actionName = (string)filterContext.RouteData.Values["action"];
string FormVeri = "";
string QueryVeri = "";
foreach (var fName in filterContext.HttpContext.Request.Form)
{
FormVeri += fName + "= " + filterContext.HttpContext.Request.Form[fName.ToString()].ToString() + "& ";
}
foreach (var fQuery in filterContext.HttpContext.Request.QueryString)
{
QueryVeri += fQuery + "= " + filterContext.HttpContext.Request.QueryString[fQuery.ToString()] + "& ";
}
base.OnResultExecuted(filterContext);
}
}
FilterConfig.cs:
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new CustomHandle());
}
}
homecontroller.cs:
public ActionResult Index()
{
return View();
}
public ActionResult Login()
{
return View();
}
customHandle 在 index 和登录上工作。但是, customhandle 是我不想在 login ActionResult 上运行。
谢谢,
最好的问候。
在MVC 5中...而不是在FilterConfig.cs
中添加操作过滤器- 将其添加到每个控制器(或基本控制器) - 所有操作都将受到影响。
- 使用[unerrideactionFilter]以特定动作去除该过滤器。
示例
[CustomHandle]
public class AnyController : Controller
{
public ActionResult Index() // has [CustomHandle] attribute
{
}
[OverrideActionFilter]
public ActionResult Login() // ignores the [CustomHandle] attribute
{
}
}
将过滤器注入控制器类时,也注入了所有操作。如果您只想将过滤器应用于一组操作,则必须向每个操作注入[CustomActionFilter]:
[CustomHandle]
public ActionResult Index()
{
...
}
public ActionResult Login()
{
...
}