如何在webapi中的过滤器中执行控制器逻辑 [英] How to execute controller logic in filters in webapi

查看:119
本文介绍了如何在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中的过滤器中执行控制器逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆