2

Is it possible to have 2 controller methods with same name ?

0

1 Answer 1

2

You're referring to controller methods which confuses me what you're talking about. Is it C# class methods or controller actions? Let me answer to both.

Pure C# doesn't allow that

It's not possible to have two methods with identical signature in C#. This means same name and same number of parameters with same types.

public int Calculate(int a, int b) { ... }
public int Calculate(int first, int second) { ... } // compiler ERROR

But Asp.net MVC controller actions allow it

If you're talking about Asp.net MVC controller actions, that's of course possible. Use ActionName attribute to accomplish what you require:

public ActionResult Common() { ... }

[ActionName("Common")]
public ActionResult CommonAgain() { ... } // C# signature differs

But there will have to be some other method selector attribute on one of these for the action invoker to know which one to use when request comes in... As they are you'd have a 404 runtime error. It may be that one should be a regular request action but the other should be executed when Ajax request comes in. Or similar. Some distinction is required.

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

3 Comments

Please change [ActionName("Sommon")] to [ActionName("Common")]
there should be [ActionName("Sommon")] or [ActionName("Common")] ?
yeah yeah... I hear you. ;) Typo... Thanks guys.

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.