0

I would like to define an extension controller method. Is this possible? When I try it, it compiles, but I receive a 404, instead of a 200.

    public static ActionResult MyMethod(this Controller)
    {
        return new EmptyResult();
    }

1 Answer 1

2

You can't have a static action method, so this won't work.

What you could do instead is create a base controller class with a public instance method:

public class BaseController : Controller
{
    public ActionResult MyMethod()
    {
        return new EmptyResult();
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I'm trying to create a module that handles some webhooks, which I want to make available via NuGet. If I require people to inherit from my base class, it will reduce utilization since you can only inherit from one class.
In that case, you may want to consider an ActionFilter.
Thanks. It looks like just writing an HttpHandler might be the way to go here, since it's more extensible. But you did answer the question as asked.

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.