Overloading Controller Actions in MVC

Had an interesting issue yesterday whereby I was getting a 404 “Sorry the resource you are looking for cannot be found” exception raised. However when I debugged the error I found that the resource in question (a razor page) was exactly where it should be! After a lot of head scratching and playing around I finally realised the exception was being raised because unlike all other C# methods you cannot overload a controller action! Hence even though my methods had different signatures MVC couldn’t distinguish between the them.

The following article suggests 3 different ways around the issue.

I chose to use the ActionName decorator which effectively allows you to set up an alternative name for the method. Bear in mind though that you also need to change your calls to the method to use the ActionName. Even still this seemed the cleanest way round the problem whilst preserving some form of overloading:

Leave a comment