Best Asp.net MVC solution - use action method selector
Why not simplify controller action methods by removing unnecessary code branch and have this kind of code as seen here:
public ActionResult Index()
{
// do something when there's no id
}
[RequiresRouteValues("id")]
public ActionResult Index(int id)
{
// do something when id is present
}
This is of course possible, as long as you provide the very simple code for RequiresRouteValuesAttribute
action method selector. You can find code in this blog post that does exactly this.
By my opinion this is the best possible solution to this problem, because:
- It simplifies code by removing unnecessary branch
- Makes code easier to maintain (due to lower complexity)
- Extends Asp.net MVC framework as it can and should
- Keeps parameter types as they should be without the need to make them nullable
- etc.
Anyway. All the details about this technique is explained in great detail in linked post.