0

Does the controllers allow overridden action methods?

For example:
Can I have two methods like:

ActionResult SendResults() { ... }
FileContentResult SendResults() { ... }
Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
Thanigainathan
  • 1,505
  • 14
  • 25

3 Answers3

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:

Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
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.

Oskar Kjellin
  • 21,280
  • 10
  • 54
  • 93
  • Actually you can http://stackoverflow.com/questions/541936/what-can-you-do-in-msil-that-you-cannot-do-in-c-or-vb-net/542011#542011 – bniwredyc Jan 27 '11 at 10:19
  • +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 Jan 27 '11 at 10:22
  • @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 – Oskar Kjellin Jan 27 '11 at 10:23
  • @bniwredyc: you can do it in MSIL, but not in C#. – Robert Koritnik Feb 28 '11 at 22:23
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");
            }
        }
frennky
  • 12,581
  • 10
  • 47
  • 63