Avoiding NULL parameter values โโ(and if )
As you saw from @Muhammad's answer (which is BTW, the one that will be accepted as the correct answer), itโs easy to get optional parameters (any route parameters actually) in the controllerโs actions. All you need to do is make sure they are invalid (because they are optional).
But since they are optional, you get branched code that is harder to support unit test. Therefore, using a simple action action selector, you can write something similar to this:
public ActionResult Index() { // do something when there not ID } [RequiresRouteValues("id")] public ActionResult Index(int id) // mind the NON-nullable parameter { // do something that needs ID }
In this case, a special action selector was used, and you can find its code and a detailed explanation in my blog post . These actions are easy to understand / understand, unit test (without unnecessary branches) and maintain.
source share