0

Does the controllers allow overridden action methods?

For example:
Can I have two methods like:

ActionResult SendResults() { ... }
FileContentResult SendResults() { ... }
2
  • You're probably talking about action methods with the same name, not actual overriding, which is part of OOP inheritance. Commented Feb 28, 2011 at 22:25
  • 1
    @Robert Koritnik Probably he thought overloading == overriding Commented Mar 1, 2011 at 10:47

3 Answers 3

2

C# impossible, Asp.net MVC action methods possible

If you can distinguish controller action methods by anything that an action method selector can separate, then it's possible to have two controller actions with the same name but with a different result:

[HttpGet]
[ActionName("SendResults")]
ActionResult SendResultsGet() { ... }

[HttpPost]
[ActionName("SendResults")]
FileContentResult SendResultsPost() { ... }

The main idea here is that you can use the ActionNameAttribute to name several action methods with the same name. Based on other action method selector attributes on these actions either of them will be executed.

When there are no out-of-the-box action method selectors that you can use you can always write your own custom one that solves your problem.

I've written two blog posts about action method selectors that may be of interest to you:

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

Comments

1

You can never have two methods only differing by return types in .Net. How would the code know which one to pick?

Consider the following code:

ActionResult result = SendResults();

It is impossible from that code to tell which method you want to invoke as FileContentResult is derived from ActionResult. You will have to do something like:

ActionResult result = SendFileContentResults();

C# bases it's signature based on the method name and parameters. To be able to create another method you have to have another signature and as the return type is not in the signature you have to change either the name or the parameters to make it compile.

4 Comments

+1, but to answer the more general question... Yes, controllers do allow overridden action methods, but overloads that differ only by return type aren't allowed in C#. (And it doesn't matter whether FileContentResult derives from ActionResult or not, the return type isn't considered when the compiler does the overload resolution.)
@LukeH I know. It was just to make it clear how it can be impossible to tell. For example it can be easy for a human to tell which method to override if one returns int and another one a string and you say that int i = returnvalue etc, but for the above method it is not possible to tell for a human
@bniwredyc: you can do it in MSIL, but not in C#.
0
  • To override - methods need to have same return types.
  • To overload - methods need to have different signatures.

If you need to return different result based on some condition, you could do something like this:

        public ActionResult SendResults()
        {
            if (somecondition)
            {
                return View();
            }
            else
            {
                return File("readme.txt", "text");
            }
        }

Comments

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.