如何在webapi中的过滤器中执行控制器逻辑

编程入门 行业动态 更新时间:2024-10-10 06:21:55
本文介绍了如何在webapi中的过滤器中执行控制器逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

I have existing logic to validate against database in place.For eg. I have two controllers, ControllerA & ControllerB, i have AuthorizeAttribute on ControllerA, Now when i get request for ControllerA action method, first my Custom AuthorizeAttribute get request it process some authorization logic and then control goes to ControllerA action method, now i want before my ControllerA action method is hit i want some validation on ControllerB to be performed and again control goes to ControllerA action method. In short i want other controller method(some logic) to be executed before actual requested action method is called.

public class ControllerA : ApiController { //some code [MyCustomAuthorizeAttribute] public SomeClass MyMethod() { return new SomeClass(); } } public class ControllerB : ApiController { //some additional existing logic here public bool IsValidUser() { //perform addtional check using existing logic return true; } } public class MyCustomAuthorizeAttribute : AuthorizeAttribute { //Some authorization logic here //I want to execute ControllerB action method IsValidUser() here //If i reieve true the control should go to ControllerA MyMethod }

我的尝试: 我们可以创建新的过滤器并应用于所需的方法,而不是更改现有的测试和部署代码。

What I have tried: Instead of changing existing tested and deployed code we can create new filter and apply on required methods.

推荐答案

而不是试图适合方形钉通过一个圆孔,你需要将你的代码重构为实际工作和有意义的东西。从属性调用控制器代码没有意义。 将IsValidUser方法拉出控制器B,将其移动到名为MyCustomAuthenticator的类,然后调用它所以 var auth = new MyCustomAuthenticator(); auth.IsValidUser(); 但是要按照你的要求回答你的问题exaclty,只需在你的属性中实例化控制器并按照这种方式调用它。这不是一个好主意。 Rather than trying to fit a square peg through a round hole, you need to refactor your code to something that will actually work and make sense. Calling controller code from an attribute doesn't make sense. Pull the IsValidUser method out of the Controller B, move it to a class called MyCustomAuthenticator and then call it like so var auth = new MyCustomAuthenticator(); auth.IsValidUser(); But to answer your question exaclty as you asked, just instantiate the controller inside your attribute and call it that way. This is not a good idea. public class MyCustomAuthorizeAttribute : AuthorizeAttribute { //Some authorization logic here //I want to execute ControllerB action method IsValidUser() here //If i reieve true the control should go to ControllerA MyMethod public void AnExampleMethodCuaseThereWasntOneHere() { var controllerB = new ControllerB(); controllerB.IsValidUser(); } }

我不知道//如果我说得对,那控件应该去到ControllerA MyMethod的意思是,听起来你想要重定向到控制器A,但你需要在控制器B中使用的代码才能这样做。 简而言之,这里更好的选择是重构你的代码,这是一个很好的例子,你会在5个月内回到它并且不知道它是怎么回事。

I don't know what "//If i reieve true the control should go to ControllerA MyMethod" means, it sounds like you want to do a redirect to Controller A but you need code used in Controller B to do so. In short, the better option here is to refactor your code, this is a good example of situations where you come back to it in 5 months and not know whats going on with it.

更多推荐

如何在webapi中的过滤器中执行控制器逻辑

本文发布于:2023-11-17 11:14:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609831.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制器   逻辑   器中   如何在   webapi

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!