Ever since I started working with ASP.NET MVC there has been one thing that has bugged me a little bit; needing to reference my strongly-typed Controllers and Action methods by their string names. It’s not a major problem for me but last night I decided to have a go at solving the problem for a project I’m currently working on. There are the obvious simple solutions such as manually creating string constants or HtmlHelper and UrlHelper extension methods. In the past I have used the extension method approach but frankly it’s a real pain to maintain going forward with updating names, removing methods, adding new methods etc. In my opinion developers shouldn’t have to worry themselves with this kind of work when we can leave it up to the IDE (Visual Studio 2012 in this case) to do this monotonous work for us.
Thanks to Visual Studio tooling and C# language features such as reflection and extension methods I have created a basic solution that suffices for my project. Note emphasised text; there is a possibility this solution may not meet your needs. I am not trying to create a comprehensive copy-and-paste solution for this article, just sharing my work so far.
Continue reading →