gpt4 book ai didi

asp.net-mvc - 在使用 [AllowAnonymous] 执行 Controller 中的 MVC 操作之前运行代码

转载 作者:行者123 更新时间:2023-12-01 11:49:26 24 4
gpt4 key购买 nike

我有一堆 Controller 在类级范围内具有 [AllowAnonymous] 属性。在执行此类 Controller 中的任何操作之前,我需要检查一些事项。例如,我可能会这样做:

[AllowAnonymous]
public class MyController : Controller
{
[HttpGet]
public ActionResult Index()
{
// do some preliminary work

// run action-specific code
}
}

因为我有很多这样的 Controller 和 Action ,复制粘贴相同的代码是一个乏味的过程。有没有办法以更简单的方式为每个匿名操作执行此初步代码?

最佳答案

您可以创建自己的操作过滤器。

创建一个继承ActionFilterAttribute并覆盖OnActionExecuting的类。
将该属性应用于 Controller 或操作,它将在对该 Controller 或操作的每个请求之前运行。

关于asp.net-mvc - 在使用 [AllowAnonymous] 执行 Controller 中的 MVC 操作之前运行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12885301/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com