- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我今天早些时候在ASP.Net MVC中使用ActionFilters
进行asked a question。原来,我的问题确实是我的ActionFilter
甚至没有运行。除其他外,我阅读了this article,但我找不到他没有做的任何事情。
这是我的代码:
// The ActionFilter itself
public class TestingIfItWorksAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.Controller.TempData["filter"] = "it worked!";
base.OnActionExecuting(filterContext);
}
}
// The Controller Action with the filter applied
[TestingIfItWorks]
public ActionResult Test()
{
var didit = TempData["filter"];
return View();
}
TempData["filter"]
保留
null
值。
最佳答案
根据您对另一个答案的评论
通过单元测试进行测试时,不调用过滤器。如果要调用过滤器,则需要模仿ControllerActionInvoker。最好单独测试过滤器,然后使用反射来确保将过滤器以正确的属性应用于您的操作。与组合测试过滤器和操作相比,我更喜欢这种机制。
原始
当然,您的方法上肯定需要一个override
,否则您实际上并没有在基类上替换该方法。我本来希望编译器提示您需要new
或override
。如果您不包括override
关键字,则其行为就像您使用了new
一样。由于框架将其作为ActionFilterAttribute调用,因此这意味着您的方法将永远不会被调用。
引用MSDN:
If the method in the derived class is not preceded by new or override keywords, the compiler will issue a warning and the method will behave as if the new keyword were present.
关于asp.net-mvc - 为什么我的所有ActionFilter都不运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/934647/
我所有的 Controller 都继承自具有 ActionFilter 属性的 BaseController: [AnalyticsData] public class BaseController
基于此链接 https://msdn.microsoft.com/en-us/library/aa664615(v=vs.71).aspx我可以传递原始类型的一维数组。但是,每当我传递一个字符串数组时
假设我有一个这样的 Controller 方法: [Audit] public JsonNetResult List(int start, int limit, string sort, string
我们有一个 ASP.NET 应用程序。我们无法编辑 Controller 的源代码。但是我们可以实现ActionFilter。 我们的 Controller 操作方法之一返回 JSON。是否可以在 A
我有一个 ActionFilterAttribute,我在其中检查有效许可证。我想返回一个 401 错误和一条消息。现在,我有以下实现: public override void OnActionEx
我正在使用 ILogger并希望它包含在我的基本 Controller 中,因为每个 Controller 都应该使用它。 我将每个派生类定义为 UsersController : BaseContr
有没有办法将非静态值传递给如下所示的 actionfilter 参数? public class ProcuctController : Controller { private int us
我打算写一个 ActionFilter用于业务验证,其中一些服务将通过服务定位器解决(我知道这不是一个好的做法,我尽可能避免使用服务定位器模式,但在这种情况下我想使用它)。 OnActionExecu
ActionFilters 和 HTTPHandlers 有什么区别?在 MVC 网络应用程序的上下文中,对用户进行身份验证。我将如何使用它们中的任何一个来执行身份验证? 最佳答案 Action 过滤
当我尝试在页面加载时向具有相同 IActionFilter 属性的 2 个 MVC 操作发出 2 个异步 ajax 请求时遇到并发问题。 SimpleInjector注册如下 public stati
我想在我的 ActionFilter 中间件中使用 DbContext。可能吗? public class VerifyProfile : ActionFilterAttribute { pu
我一直试图找到一些关于此的直接信息,但我一直无法找到 - 例如,我发现的内容对于我理解正在发生的事情来说含糊不清,或者对于错误的事情来说太具体了我找到的用于对 AuthorizeAttribute 进
我想听听您的意见,也许对以下情况有更好的建议: 我已经定义了一个自定义的 ActionFilter 来完成一些工作并产生一些值(value)。我想在 Controller 操作和模型中使用该值。 现在
有没有一种方法可以为 WCF 创建自定义方法属性,让我可以轻松地使用预过滤器装饰服务方法,就像 MVC 使用操作过滤器一样。我计划将它们用于身份验证和授权。这适用于请求携带身份验证 cookie 的
我正在尝试使用 StructureMap 来学习本教程:http://iridescence.no/post/Constructor-Injection-for-ASPNET-MVC-Action-F
当OnActionExecuting中满足特定条件时,是否可以跳过整个操作方法执行并返回特定的ActionResult? 最佳答案 您可以为此使用filterContext.Result。它应该看起来
从 ActionFilter 中止/取消操作的最佳方式 我有这个 ActionFilter,它应该立即结束连接并返回 401 Unauthroized: public class SignInRequ
我们可以像这样将 IConfiguration 注入(inject)到类中: //Controller AppSettings obj = new AppSettings(_configuration
我有一个在 ASP.NET MVC 2 中运行 OnActionExecuting 的操作过滤器。本质上,我希望操作过滤器能够清理我的数据并替换当前模型(它将传递给后续操作过滤器以及我的操作方法)净化
我们是否应该将本应位于 Controller 中的逻辑(如渲染局部 View 的数据)移至 ActionFilter 中? 例如,我正在制作一个 CMS 网站。应该有一个广告 block 要呈现在几个
我是一名优秀的程序员,十分优秀!