3

Take a look at the code below.

When Get() calls Foobar() and Foobaz(), the ActionFilters that decorate Foobar() and Foobaz() are not invoked.

How can I call these other two controller actions from within Get() in a way that also causes the SomeAction and AnotherAction filters to execute?

public class Features : Controller
{
    [SomeAction]
    public bool Foobar(string id = null)
    {
        return true;
    }

    [AnotherAction]
    public bool Foobaz()
    {
        return false;
    }

    public JsonResult Get()
    {
        return this.JsonResult(new Dictionary<string, bool>() 
        {
            { "foobar", this.Foobar() },
            { "foobaz", this.Foobaz() }
        });
    }
}

1 Answer 1

4

Controller actions aren't meant to be used as methods. In order for them to function properly they must be called within the request cycle. Perhaps your example is too simplistic, but it looks like you're trying to use these "actions" as just standard methods. While a controller can technically have a method that's never actually meant to be exposed as a route, it's not a good idea to do this sort of thing. Move the logic off into your model or some helper class.

Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately, all this code is built wrong and I do not have the opportunity to correct and do things right. I somehow have to do what's shown, even if it means that Get() has to programmatically generate two HTTP requests that each spin up indenepdent controller instances. :(
If you're absolutely stuck doing it this way, I would suggest treating it like an API action and pull the data with HttpClient or similar.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.