Reputation: 1547
Does the controllers allow overridden action methods?
For example:
Can I have two methods like:
ActionResult SendResults() { ... }
FileContentResult SendResults() { ... }
Upvotes: 0
Views: 4008
Reputation: 105009
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:
Upvotes: 2
Reputation: 13934
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");
}
}
Upvotes: 0
Reputation: 21860
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.
Upvotes: 1