使用属性提前从方法返回
是否可以创建一个属性来检查条件,然后根据该条件停止执行装饰方法或让装饰方法继续执行?
现在是显而易见的问题。如果是这样,怎么办?如果没有,那么是否有值得一试的解决方法?
此属性的总体目标是在 ASP.NET MVC 中检查用户是否已获得授权,如果未获得授权,则返回特定的 JsonResult(在属性中定义),或者让装饰方法继续执行。我看到的一个明显的问题是,如果控制器操作的类型与 ActionResult
或 JsonResult
不同,则可能存在运行时错误。
Is it possible to make an attribute that checks a condition and then based on that condition either stop the decorated method from executing or let the decorated method continue on executing?
And now the obvious question. If so, how? If not, then is there a work around worth checking out?
The overall goal of this attribute is to, in ASP.NET MVC, check if a user is authorized and either return a particular JsonResult
(defined in the attribute) if they aren't authorized or let the decorated method keep on executing. The obvious problem I see with this being possible is if the controller action is of a different type than ActionResult
or JsonResult
that there exists the possibility of a runtime error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于您的特定情况,您可以使用 ASP.NET MVC 过滤器来执行您需要的操作。让您的属性扩展 AuthorizeAttribute。
For your particular situation, you can use an ASP.NET MVC filter to do what you need. Have your attribute extend AuthorizeAttribute.
使用属性这是不可能的 - 属性在编译时解析。它们不会在运行时执行。
属性为“控制”的特定行为是通过反射来管理的。
This is not possible using attributes - attributes are resolved at compile time. They do not get executed during runtime.
Specific behavior that attributes "control" is managed through reflection.