I know there a many Q&A's on here regarding MVC routing. Unfortunately, I am still having some issues regarding routing that I don't understand.
First and Foremost - Can I have onverloaded ActionResults in my Controller?
For example, I make a 3 requests to the same controller/action with different parameters.
- "/MyController/DoSomething"
- "/MyController/DoSomething/[Guid String]
- "/MyController/DoSomething/[Guid String]/TypeA
... and in the controller I have these methods
public ActionResult DoSomething()
{
ViewBag.Title = "Do Something By Default";
return View();
}
public ActionResult DoSomething(String id)
{
ViewBag.Title = "Do Something By id";
return View();
}
public ActionResult DoSomething(String id, String type)
{
ViewBag.Title = "Do Something By id and type";
return View();
}
I would expect each overloaded method to do it's thing - but it doesn't. The only ActionMethod being invoked is DoSomething() regardless of the parameters I enter. In short, the DoSomething(String id) and DoSomething(String id, String type) are completely ignored.
Even when I attempted this...
public ActionResult DoSomething(String? id)
{
ViewBag.Title = "Do Something Conditionally";
return View();
}
...just to see what would happen. A compilation error is thrown
Error 4 The type 'string' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable'
NOTE: My parameters are strings (technically GUIDs) - not integers.
I figured perhaps I needed to map these routes in the /App_Start/RouteConfig class and tried this just to see if I could get one overloaded method to work
routes.MapRoute(
"MyController",
"MyController/DoSomething/{id}",
new { controller = "MyController", action = "DoSomething", id = "" }
);
...and still the DoSomething(String id) ActionResult was not invoked. No errors, no exceptions. It was if I did not even have that MapRoute specified.
Is there something I am missing here? Any insight, instruction, guidance you can provide would be greatly appreciated.